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!
source to share
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.
source to share
There is a JSONP method that is used to work around this. See second answer SO # 570100
source to share