简体   繁体   English

我可以通过AJAX从外部页面加载数据吗?

[英]Can I load data from an external page via AJAX?

I've just starting learning jQuery and AJAX. 我刚开始学习jQuery和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. 我可以通过jQuery.load()将本地页面(在我的磁盘上)加载到div中,但外部站点似乎不起作用。 I've even used wireshark to check if the data is being sent from the server (it is). 我甚至使用wireshark来检查数据是否从服务器发送(它是)。 Sample code is below: 示例代码如下:

<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? 如果是这样,怎么样?

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/");

?>

Out of the box: no. 开箱即用:没有。 It's a security issue. 这是一个安全问题。 There are a few different workarounds though. 但是有一些 不同的 解决方法

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. 您无法使用AJAX从外部域访问数据,这被视为安全风险。 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请求使用浏览器存储的cookie - 如果我尝试访问facebook.com,并且您已登录,则会发送cookie并且我可以访问您的个人数据。

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) (如果您从file://运行页面,则无法执行此file:// url)

Ajax? 阿贾克斯? Yes. 是。 XHR? XHR? No (unless the browser implements Cross-site XHR which isn't widespread yet). (除非浏览器实现了尚未普及的跨站点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而言)。

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 . 您尝试请求的站点需要启用JSONP才能工作,并且您将使用跨域回调 Alternatively, you could create a proxy on your own domain which grabs the page on behalf of your ajax request. 或者,您可以在自己的域上创建代理,代表您的ajax请求抓取页面。

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解决方案

You can't call Ajax from another domain. 您无法从其他域调用Ajax。 Check JSON technique for this 检查JSON技术

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM