How to use jQuery AJAX for an outside domain?

后端 未结 5 1201
闹比i
闹比i 2020-12-11 11:49

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         


        
相关标签:
5条回答
  • 2020-12-11 12:11

    Javascript cannot access a server outside of where the javascript file came from.

    That is a security feature.

    Depending on how browser-specific you want to get you may get around this, but that becomes a bit of a slippery slope.

    0 讨论(0)
  • 2020-12-11 12:17

    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.
    0 讨论(0)
  • 2020-12-11 12:19

    There's a method called JSONP which is used to circumvent that. See the 2nd reply on SO #570100

    0 讨论(0)
  • 2020-12-11 12:21

    You cannot do cross domain ajax requests directly, this would be a security concern.

    You will need to call your local php file from jquery and have the php file talk to the other domain.

    0 讨论(0)
  • 2020-12-11 12:23

    It's true that you normally can't do Ajax outside your domain due to the browsers. However using JSONP it is possible to do this. jQuery also has a jsonp param for Ajax now. To make this work you need to control the output of the server though.

    0 讨论(0)
提交回复
热议问题