I've just starting learning jQuery and AJAX. I'm able to load a local page (on my disk) into a div via jQuery.load()
, but external sites don't seem to work. I've even used wireshark to check if the data is being sent from the server (it is). Sample code is below:
我刚开始学习jQuery和AJAX。我可以通过jQuery.load()将本地页面(在我的磁盘上)加载到div中,但外部站点似乎不起作用。我甚至使用wireshark检查数据是否从服务器发送(它是)。示例代码如下:
<html>
<head>
<script src='jquery-1.4.2.min.js'></script>
<script>
$(document).ready(function() {
// $('#test').load('localpage.htm'); works!
$('#test').load('http://www.google.com/'); // does not work!
});
</script>
</head>
<body>
<div id='test'></div>
</body>
</html>
Is it possible to do this in the first place? If so, how?
首先可以做到这一点吗?如果是这样,怎么样?
8 个解决方案
#1
3
Out of the box: no. It's a security issue. There are a few different workarounds though.
开箱即用:没有。这是一个安全问题。但是有一些不同的解决方法。
#2
4
You cannot do ajax calls to a different domain than the script originates from.
您不能对脚本来源之外的其他域执行ajax调用。
For doing such a thing, you have to use a proxy page on your own page, eg:
要做这样的事情,你必须在自己的页面上使用代理页面,例如:
<script>
$(document).ready(function() {
$('#test').load('ajax/getgoogle.php');
});
</script>
getgoogle.php:
getgoogle.php:
<?php
echo file_get_contents("http://www.google.com/");
?>
#3
2
You're running into the Same Origin Policy. You can't access data from an external domain using AJAX, it's considered a security risk. The reasoning behind it is that AJAX requests work with cookies stored by the browser -- if I tried to access facebook.com, and you were logged in there, the cookie would be sent and I'd have access to your personal data.
你正在遇到同源政策。您无法使用AJAX从外部域访问数据,这被视为安全风险。其背后的原因是AJAX请求使用浏览器存储的cookie - 如果我尝试访问facebook.com,并且您已登录,那么cookie将被发送并且我可以访问您的个人数据。
#4
2
For security reasons, you cannot use AJAX to request a page from a different domain (or protocol or port).
出于安全原因,您无法使用AJAX从不同的域(或协议或端口)请求页面。
Instead, you can write a server-side script on your server to forward requests to another domain. (This is not possible if you're running a page from a file://
url)
相反,您可以在服务器上编写服务器端脚本以将请求转发到另一个域。 (如果您从文件中运行页面,则无法执行此操作:// url)
#5
2
Ajax? Yes. XHR? No (unless the browser implements Cross-site XHR which isn't widespread yet).
阿贾克斯?是。 XHR?否(除非浏览器实现了尚未普及的跨站点XHR)。
To get the data with Ajax without using XHR the external site must provide the data in the JSONP format.
要在不使用XHR的情况下使用Ajax获取数据,外部站点必须提供JSONP格式的数据。
Alternatively, you can proxy the data through a server side script on your server, thus making it come from the same host (as far as JavaScript is concerned).
或者,您可以通过服务器上的服务器端脚本代理数据,从而使其来自同一主机(就JavaScript而言)。
#6
2
No, it's not. Have a look at Same Origin Policy. The site you are trying to request would need to have JSONP enabled for that to work, and you would utilize a cross-domain callback. Alternatively, you could create a proxy on your own domain which grabs the page on behalf of your ajax request.
不,这不对。看看同源政策。您尝试请求的站点需要启用JSONP才能工作,并且您将使用跨域回调。或者,您可以在自己的域上创建代理,代表您的ajax请求抓取页面。
#7
1
Load this PHP script instead of trying to load website directly
加载此PHP脚本,而不是尝试直接加载网站
$filename = "http://www.sitename.com";
$handle = fopen($filename, "r");
if ($handle)
{
while (!feof($handle))
{
$text .= fread($handle, 128);
}
fclose($handle);
}
print $text;
Edit: Or simply like henchman's solution with file_get_contents
编辑:或者只是喜欢henchman的file_get_contents解决方案
#8
0
You can't call Ajax from another domain. Check JSON technique for this
您无法从其他域调用Ajax。检查JSON技术
#1
3
Out of the box: no. It's a security issue. There are a few different workarounds though.
开箱即用:没有。这是一个安全问题。但是有一些不同的解决方法。
#2
4
You cannot do ajax calls to a different domain than the script originates from.
您不能对脚本来源之外的其他域执行ajax调用。
For doing such a thing, you have to use a proxy page on your own page, eg:
要做这样的事情,你必须在自己的页面上使用代理页面,例如:
<script>
$(document).ready(function() {
$('#test').load('ajax/getgoogle.php');
});
</script>
getgoogle.php:
getgoogle.php:
<?php
echo file_get_contents("http://www.google.com/");
?>
#3
2
You're running into the Same Origin Policy. You can't access data from an external domain using AJAX, it's considered a security risk. The reasoning behind it is that AJAX requests work with cookies stored by the browser -- if I tried to access facebook.com, and you were logged in there, the cookie would be sent and I'd have access to your personal data.
你正在遇到同源政策。您无法使用AJAX从外部域访问数据,这被视为安全风险。其背后的原因是AJAX请求使用浏览器存储的cookie - 如果我尝试访问facebook.com,并且您已登录,那么cookie将被发送并且我可以访问您的个人数据。
#4
2
For security reasons, you cannot use AJAX to request a page from a different domain (or protocol or port).
出于安全原因,您无法使用AJAX从不同的域(或协议或端口)请求页面。
Instead, you can write a server-side script on your server to forward requests to another domain. (This is not possible if you're running a page from a file://
url)
相反,您可以在服务器上编写服务器端脚本以将请求转发到另一个域。 (如果您从文件中运行页面,则无法执行此操作:// url)
#5
2
Ajax? Yes. XHR? No (unless the browser implements Cross-site XHR which isn't widespread yet).
阿贾克斯?是。 XHR?否(除非浏览器实现了尚未普及的跨站点XHR)。
To get the data with Ajax without using XHR the external site must provide the data in the JSONP format.
要在不使用XHR的情况下使用Ajax获取数据,外部站点必须提供JSONP格式的数据。
Alternatively, you can proxy the data through a server side script on your server, thus making it come from the same host (as far as JavaScript is concerned).
或者,您可以通过服务器上的服务器端脚本代理数据,从而使其来自同一主机(就JavaScript而言)。
#6
2
No, it's not. Have a look at Same Origin Policy. The site you are trying to request would need to have JSONP enabled for that to work, and you would utilize a cross-domain callback. Alternatively, you could create a proxy on your own domain which grabs the page on behalf of your ajax request.
不,这不对。看看同源政策。您尝试请求的站点需要启用JSONP才能工作,并且您将使用跨域回调。或者,您可以在自己的域上创建代理,代表您的ajax请求抓取页面。
#7
1
Load this PHP script instead of trying to load website directly
加载此PHP脚本,而不是尝试直接加载网站
$filename = "http://www.sitename.com";
$handle = fopen($filename, "r");
if ($handle)
{
while (!feof($handle))
{
$text .= fread($handle, 128);
}
fclose($handle);
}
print $text;
Edit: Or simply like henchman's solution with file_get_contents
编辑:或者只是喜欢henchman的file_get_contents解决方案
#8
0
You can't call Ajax from another domain. Check JSON technique for this
您无法从其他域调用Ajax。检查JSON技术