AJAX Cross Domain Request

I have my JavaScript files in my main domain, and I want to make some calls from a subdomain.

I added:

url: "http://domain.com/ajax.php" 

So the full code:

  $.ajax({ type: "POST", url: "http://domain.com/ajax.php", data: { var1: var1, var2: var2 }, success: function(data){ } }); 

But in Firebug, it shows the request as red , and it does not respond. POST parameters are also where they should be.

Should I create a new JS file on a subdomain and add the necessary codes and call AJAX from there?

EDIT: using JSONP code

I use this on localhost/ajax.php , which I call from sub.localhost

  $.ajax({ dataType: 'jsonp', data: 'id=10', jsonp: 'jsonp_callback', url: 'http://localhost/ajax.php', success: function (data) { console.log(data); }, }); 

and ajax.php contains:

 <?php echo $_GET["id"]; ?> 
0
source share
3 answers

You can use the Access-Control-Allow-Origin header to enable cross-domain requests.

Read the following: Resource Sharing (CORS)

+4
source

Assuming you have jQuery 1.5+, you can use:

 $.ajax({ crossDomain:true, type: "POST", url: "http://domain.com/ajax.php", data: { var1: var1, var2: var2 }, success: function(data){ } }); 

From DOCS:

crossDomain (added 1.5)

Default: false for same-domain requests, true for cross-domain requests

If you want to force the crossDomain request (for example, JSONP) in the same domain, set the crossDomain parameter to true. This allows, for example, to redirect the server side to another domain

+2
source

For subdomain calls, you have two options:

0
source

All Articles