Jayshit Ladani
Jayshit Ladani

Reputation: 143

How to make jQuery ajax call between sub-domains?

I have a domain like below.

I have two sub-domains within that as below.

http://foo.jayshit.info

http://bar.jayshit.info

I want to make a jQuery ajax call from foo.jayshit.info to bar.jayshit.info to get some html or to update some data back to server.

I know due to same origin policy its not that easy. and i do not want to use JSONP as well. because i want my ajax call to have async: false as below.

$.ajax({
    type: "POST",
    cache: false,
    url: tURL,
    data: tData,
    async: false
});

Please let me know if anyone came across some workaround for this.

Thanks in advance.

Regards,

Upvotes: 3

Views: 6248

Answers (5)

Chris Love
Chris Love

Reputation: 3893

The answer is both CORS and JSONP. IE does not support CORS yet, you will have to wait for IE10 to get that. JSONP only allows you to do cross domain GET not POST, PUT and DELETE. CORS does solve those problems and it is supported by all other browsers.

The other way you can solve this is to build a proxy in your website that will make the call from the server for you. This will get you around the cross domain issues in the browser. Those are in place as a security precaution in case you were wondering.

Upvotes: 0

curmudgeon
curmudgeon

Reputation: 145

I suggest you add the following to your code:

jQuery.support.cors = true;

See this link for further information

Upvotes: 0

V1tOr
V1tOr

Reputation: 369

use

dataType: "jsonp"

...

$.ajax({
    type: "POST",
    dataType: "jsonp",
    cache: false,
    url: tURL,
    data: tData,
    async: false
});

more info:

http://api.jquery.com/jQuery.ajax/

Upvotes: 0

dark_gf
dark_gf

Reputation: 736

http://foo.jayshit.info and http://bar.jayshit.info is 2 different domains,so u need cross domain request, first link from google: http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/

Upvotes: 0

Esailija
Esailija

Reputation: 140228

Since you are in control of the other sub-domain, you can send this header from bar.jayshit.info:

Access-Control-Allow-Origin: http://foo.jayshit.info
Access-Control-Allow-Methods: GET, POST

Modern browsers respect these headers and allow you to make the request. For IE8 you need to use XDomainRequest which is not supported by jQuery.

Upvotes: 5

Related Questions