0
我試圖訪問使用AJAX多臺服務器的資源,和我遇到了這樣的問題:的XMLHttpRequest無法加載
XMLHttpRequest cannot load http://first.mydomain.com. Origin http://second.mydomain.com is not allowed by Access-Control-Allow-Origin.
用下面的代碼
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);
}
我已經添加了以下內容響應頁面被請求。
header('Access-Control-Allow-Origin: *');
我試圖明確命名請求者也沒有成功。
感謝
PS:上面的代碼我肯定ins't完美,但功能罰款時,只是想請求主機服務器的資源。
經典。這是因爲[同源策略](http://en.wikipedia.org/wiki/Same_origin_policy) –
好的,但有沒有解決方法? – 111111
是的,通過JSONP回調和'