类似于php文件的Javascript方法
在javascript或jquery中是否有类似于file_getcontents的方法类似于php文件的Javascript方法,javascript,jquery,Javascript,Jquery,在javascript或jquery中是否有类似于file_getcontents的方法 <?php $html = file_get_contents('http://m.uploadedit.com/b037/1405919727889.txt'); echo ($html);?> 但我什么也得不到。有什么建议吗?你不能跨域请求,你需要设置一个php代理,比如,在你的服务器上创建一个php文件,比如说get\u contents.php $html = file_get_c
<?php $html = file_get_contents('http://m.uploadedit.com/b037/1405919727889.txt');
echo ($html);?>
但我什么也得不到。有什么建议吗?你不能跨域请求,你需要设置一个php代理,比如,在你的服务器上创建一个php文件,比如说
get\u contents.php
$html = file_get_contents('http://m.uploadedit.com/b037/1405919727889.txt');
echo ($html);
在jquery中,访问您的php,如下所示:
$(document).ready(function () {
$.ajax({
url:"http://your_server.com/get_contents.php",
type: "GET",
success: function (data) {
alert(data)
}
});
});
Javascript无法获取文件内容,但正如您所见,php可以。我建议你和togeter一起工作
$(document).ready(function () {
$.ajax({
url:"http://m.uploadedit.com/content/somehash",
type: "Get",
success: function (data) {
alert(data)
}
});
});
在“/content/somehash”中放入一个php文件:
<?php echo file_get_contents('http://m.uploadedit.com/content/somehash');
这里有一个解决方案。我发现这个很好用
$( document ).ready(function() {
$.ajaxPrefilter(function(options) {
if(options.crossDomain && jQuery.support.cors) {
var http = (window.location.protocol === 'http:' ? 'http:' : 'https:');
options.url = http + '//cors-anywhere.herokuapp.com/' + options.url;
//options.url = "http://cors.corsproxy.io/url=" + options.url;
}
});
$.get(
'http://m.uploadedit.com/b037/1405919727889.txt',
function(response) {
$("#content").html(response);
alert(response);
});
您无法使用html访问跨域文件,您需要服务器端scripting@pareshm,为什么不能?请看@jQueryAngryBird,但我在这里做错了什么?你没有做错任何事情,在chrome上运行它会在ConsoleAnks上给你错误我知道如何在php中做,但我想在php中做同样的事情jquery@user3754380但是,伙计,你不能…这就像要求画两条相互不接触的垂直线…@user3754380你不能使用jsi访问不同域上的文件我不想使用服务器端编程,否则我会自己做我正在寻找客户端options@user3754380,jQuery没有获得那么多权限:)对不起, ... 如果您不想要服务器端。。。为什么要向服务器请求某些内容?为什么要进行http调用?请阅读我的问题,我要求您选择服务器端。客户端不能做您需要的事情。所以为什么不需要服务器端?
fs.readFile('/content/somehash', function (err, data) {
if (err) throw err;
// here you can output the content ...
});
$( document ).ready(function() {
$.ajaxPrefilter(function(options) {
if(options.crossDomain && jQuery.support.cors) {
var http = (window.location.protocol === 'http:' ? 'http:' : 'https:');
options.url = http + '//cors-anywhere.herokuapp.com/' + options.url;
//options.url = "http://cors.corsproxy.io/url=" + options.url;
}
});
$.get(
'http://m.uploadedit.com/b037/1405919727889.txt',
function(response) {
$("#content").html(response);
alert(response);
});