I am trying to access resourced from multiple servers using AJAX, and I am running into this problem:
XMLHttpRequest cannot load http://first.mydomain.com. Origin http://second.mydomain.com is not allowed by Access-Control-Allow-Origin.
With the following code
for ( i in domains )
{
var url = 'http://'+domains[i]+'/mgmt/json/queue_status.php';
requests[i]=new request(url);
break;
}
function request(url)
{
var queues = {};
http_request = new XMLHttpRequest();
http_request.open("GET", url, true, 'username', 'password');
http_request.onreadystatechange = function () {
var done = 4, ok = 200;
if (http_request.readyState == done && http_request.status == ok) {
queues = JSON.parse(http_request.responseText);
var queuesDiv = document.getElementById('queues');
print_queues(queues, queuesDiv);
}
}
http_request.send(null);
}
开发者_JAVA技巧
I have added the following to response page being requested.
header('Access-Control-Allow-Origin: *');
I have tried explicitly naming the requester too with no success.
Thanks
PS: The above code I am sure ins't perfect but function fine when only trying to requests the resource of the host server.
username and password are not allowed in cross origin requests.
Throws an INVALID_ACCESS_ERR exception if either user or password is passed as argument and the origin of url does not match the XMLHttpRequest origin.
source: http://www.w3.org/TR/XMLHttpRequest2/#the-open-method
Just pass the password and username as a get variable instead.
精彩评论