How do I use jQuery AJAX for an external domain?

I am trying to do a simple jQuery AJAX attempt using either .get () or .post ().

If I have a local copy on my server and do:

$.get('/hash.php',...,...)

      

I track in my console in firebug that get is being executed and I get a response.

All I change is the URI for the external server and nothing happens.

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

      

Doesn't help if I take his 's' or if I use the post instead. Am I missing some parameter that I should be using in jQuery?

EDIT: I forgot to mention the reason I am doing this because I am eventually moving from a PHP4 site to a PHP5 site, but right now a real PHP4 site needs a function that is not in PHP4. So I am calling a PHP5 server to do this. I think I have a good workaround. Thank!

+2


source to share


5 answers


You cannot send an Ajax request to a different domain than another where your application is deployed. This is due to the Same Origin Policy implemented in web browsers - a security measure.

There are two possible solutions:



  • sending a request to your own server, which will act as a proxy to another (either via a PHP script, or better, using some of Apache's mod_proxy_http

    )
  • or not use "Ajax", but other techniques such as dynamically generating tags <script>

    that do not fall under the SOP limitation.
+3


source


It is true that you usually cannot do Ajax outside of your domain due to browsers. However, using JSONP it can be done. JQuery now also has a jsonp parameter for Ajax. To get this work done, you need to manage the server output.



+3


source


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

This is a safety feature.

Depending on how you will be accessing the browser, you might be able to get around this, but it gets a little slippery.

+1


source


You cannot directly make cross domain ajax requests, it will be a security issue.

You will need to call your local php file from jquery and talk to a php file with a different domain.

+1


source


There is a JSONP method that is used to work around this. See second answer SO # 570100

+1


source







All Articles