I have my JavaScript files on my main domain and I want to do some calls from the subdomain.
I have added:
url: "http://domain.com/ajax.php"
So the full code is:
$.ajax({
type: "POST",
url: "http://domain.com/ajax.php",
data: {
var1: var1,
var2: var2
},
success: function(data){
}
});
But on Firebug it shows the request as red and it fails to respond. Also the POST parameters are there as they should.
Should I create a new JS file on the subdomain and add the necessary codes and do from there the AJAX calls?
EDIT: using JSONP code
I am using 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 the ajax.php contains:
<?php
echo $_GET["id"];
?>
You can use
Access-Control-Allow-Originheader to enable cross-domain requests.Read this: Cross-Origin Resource Sharing (CORS)