Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

php - How to use jQuery AJAX for an outside domain?

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',...,...)

I monitor in my console in firebug that the get is done and I get a response.

All I change is the URI to an outside server and nothing happens.

$.get('https://secure.mysite.com/subdir/hash.php',...,...)

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?

EDIT: 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 :

  • 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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...