2014-02-12

In Cross Domain AJAX jQuery tutorial, I have covered how to send Cross domain AJAX requests without CORS using jQuery XDomain Library.
XDomain is a Pure CORS (Cross Origin Resource Sharing) javascript library, So you need to enable CORS on your server.

What problems it will solve?
1) You need not to add any CORS configuration to server
2) You need not to handle XDomainRequest specially for IE
3) It support HTTP Methods GET and POST
4) It is supported by all the browsers except IE7,IE8.

How it works ?
Below image explains how XDomain works.


1) XDomain creates an iframe in Master(client.com) which point to Slave(server.com)
2) client.com will communicate to server.com iframe using window.postMessage API
3) server.com iframe sends AJAX request and returns the results to client.com

 

How you can send Cross Domain AJAX Requests?
If you want to send an AJAX request from http://client.com (Master) to http://server.com (Slave), you need to follow the steps.

Step 1) You need to create Proxy (proxy.html) on Slave(server.com) domain
Below is the content of the proxy.html

The below code is equivalent to above.

You can configure multiple masters in proxy.html using xdomain.masters() API

 

Step 2) In master(client.com) domain webpage you need to add the below code.

Note: You need to include the script before jQuery library.

 

Step 3) Now You can make Cross Domain AJAX requests from Master(client.com).

Below code sends AJAX request to http://www.whoisdomain.me



The post Cross Domain AJAX jQuery without CORS appeared first on hayaGeek.

Show more