AJAX跨域调用 [英] AJAX cross domain call

查看:186
本文介绍了AJAX跨域调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道AJAX跨域策略。 所以,我不能只叫 http://www.google.com 通过AJAX HTTP请求和显示器 结果地方上我的网站。

I know about AJAX cross-domain policy. So I can't just call "http://www.google.com" over a ajax HTTP request and display the results somewhere on my site.

我试了一下由于数据类型JSONP,这实际上是将工作,但我得到一个语法错误(显然是因为接收到的数据是不是JSON格式化)

I tried it with dataType "jsonp", that actually would work, but I get a syntax error (obviously because the received data is not JSON formated)

有没有其他的方法可行,从外部域接收/显示数据? iFrame中遵循同样的政策?

Is there any other possiblity to receive/display data from a foreign domain? iFrames follow the same policy?

推荐答案

唯一的(简单)的方式使用AJAX是使用服务器端语言作为代理为的Andyê指出。下面是一个小样本如何实现使用jQuery:

The only (easy) way to get cross-domain data using AJAX is to use a server side language as the proxy as Andy E noted. Here's a small sample how to implement that using jQuery:

jQuery的一部分:

The jQuery part:

$.ajax({
    url: 'proxy.php',
    type: 'POST',
    data: {
        address: 'http://www.google.com'
    },
    success: function(response) {
        // response now contains full HTML of google.com
    }
});

和PHP的(proxy.php):

And the PHP (proxy.php):

echo file_get_contents($_POST['address']);

就这么简单。要知道,你可以或不能与刮下的数据做什么。

Simple as that. Just be aware of what you can or cannot do with the scraped data.

这篇关于AJAX跨域调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆