views:

308

answers:

5

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!

+1  A: 

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.

James Black
+2  A: 

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.
Pascal MARTIN
You could use `jsonp`, which is just a glorified version of the `<script>` tag solution you mentioned.
Dan Herbert
+1  A: 

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.

kerchingo
+2  A: 

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.

googletorp
A: 

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

T. Stone