如何使用jQuery AJAX的外域? [英] How to use jQuery AJAX for an outside domain?
问题描述
我想执行使用一个获得()或。员额()一个简单的jQuery AJAX的尝试。
I am trying to perform a simple jQuery AJAX attempt using either a .get() or a .post().
如果我有一个本地副本我的服务器上做:
If I have a local copy on my server and do:
$.get('/hash.php',...,...)
我监视我在Firebug控制台该得到的是做,我得到回应。
I monitor in my console in firebug that the get is done and I get a response.
我所改变的是URI到外部服务器,并没有任何反应。
All I change is the URI to an outside server and nothing happens.
$.get('https://secure.mysite.com/subdir/hash.php',...,...)
没有帮助,如果我走了'S'关闭或者,如果我使用POST。我失去了我应该在jQuery中使用的一些参数?
Doesn't help if I take the 's' off or if I use post instead. Am I missing some parameter that I should use in jQuery?
编辑:我忘了,更不用说我这样做是因为我终于从PHP4网站到PHP5站点迁移的原因,但对于现在的生活PHP4网站需要的功能,是不是在PHP4。所以我打电话一个PHP5服务器来做到这一点。我想我有一个很好的解决方法。谢谢!
I forgot to mention the reason I'm doing this is because I am eventually migrating from a PHP4 site to a PHP5 site, but for now the live PHP4 site needs a function that isn't in PHP4. So I am calling a PHP5 server to do it. I think I have a good workaround. Thanks!
推荐答案
您不能发送一个Ajax请求不是在您的应用程序部署在其他的另一个域。这是因为href="http://en.wikipedia.org/wiki/Same%5Forigin%5Fpolicy" rel="nofollow">同源政策在网上browers实现的
You cannot send an Ajax Request to another domain than the other on which your application is deployed. This is because of the Same Origin Policy implemented in web-browers -- a security measure.
有两种可能的解决方案,但:
There are two possible solutions, though :
- 将请求发送到您自己的服务器,这将作为另一个代理(通过一个PHP脚本,或者更好的,使用一些Apache的的
mod_proxy_http
模块) - 或不使用Ajax的,但其他技术,如动态创建
<脚本>
标签 - 这是不受SOP约束
- sending the request to your own server, that will act as a proxy to another (either via a PHP script, or, better, using some of Apache's
mod_proxy_http
module) - or not using "Ajax", but other techniques, like dynamically creating
<script>
tags -- which are not subject to the SOP constraint.
这篇关于如何使用jQuery AJAX的外域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!