How to use jQuery AJAX for an outside domain?(如何将 jQuery AJAX 用于外部域?)

本文介绍了如何将 jQuery AJAX 用于外部域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 .get() 或 .post() 执行简单的 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 请求发送到部署应用程序的其他域以外的其他域.这是因为在网络浏览器中实施了同源政策——一种安全措施.

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",而是使用其他技术,例如动态创建