AJAX 跨域调用

编程入门 行业动态 更新时间:2024-10-10 21:25:55
本文介绍了AJAX 跨域调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我了解 AJAX 跨域策略.所以我不能通过 ajax HTTP 请求和显示调用www.google"结果在我网站上的某个地方.

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

我用 dataType "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 E 指出.这是一个如何使用 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 部分:

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

还有 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 跨域调用

本文发布于:2023-11-28 12:45:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642454.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:AJAX

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!