你如何获得与.load从另一个域的内容()? [英] How do you get content from another domain with .load()?

查看:106
本文介绍了你如何获得与.load从另一个域的内容()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请求我与.load()(或任何jQuery的AJAX功能)域从任何位置的数据工作得很好。

Requesting data from any location on my domain with .load() (or any jQuery ajax functions) works just fine.

试图访问一个不同域的网址无法正常工作,虽然。你怎么办呢?其他领域也恰好是我的。

Trying to access a URL in a different domain doesn't work though. How do you do it? The other domain also happens to be mine.

我读到一招,你可以用PHP,使一个代理服务器获取内容做,那么你使用jQuery的AJAX功能,在你的服务器上的PHP的位置,但仍然使用jQuery的AJAX自己的服务器上,这样没有按T细胞计数。

I read about a trick you can do with PHP and making a proxy that gets the content, then you use jQuery's ajax functions, on that php location on your server, but that's still using jQuery ajax on your own server so that doesn't count.

有没有好的插件吗?

编辑:我发现jQuery的一个很好的插件,可以让你以同样的方式使用任何jQuery函数来请求其他页面的内容,你会在你自己的域名正常的Ajax请求

I found a very nice plugin for jQuery that allows you to request content from other pages using any of the jQuery function in just the same way you would a normal ajax request in your own domain.

该职位:<一href="http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/">http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/

该插件:<一href="https://github.com/jamespadolsey/jQuery-Plugins/tree/master/cross-domain-ajax/">https://github.com/jamespadolsey/jQuery-Plugins/tree/master/cross-domain-ajax/

推荐答案

这是因为跨域策略,其中,在排序,意味着使用客户端脚本(即JavaScript的......),你不能请求从另一个域数据。我们是幸运的,这个限制不会在大多数服务器端脚本存在。

This is because of the cross-domain policy, which, in sort, means that using a client-side script (a.k.a. javascript...) you cannot request data from another domain. Lucky for us, this restriction does not exist in most server-side scripts.

所以...

JavaScript的:

Javascript:

$("#google-html").load("google-html.php");

在谷歌html.phpPHP:

PHP in "google-html.php":

echo file_get_contents("http://www.google.com/");

是可行的。

这篇关于你如何获得与.load从另一个域的内容()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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