我正在使用javascript的XMLHttpRequest對象向其他頁面發送請求(不在同一臺服務器或域名上)我在Firefox中遇到ns_error_failure錯誤,但Javascript在Google Chrome中工作這似乎是因爲Firefox的XSS政策。跨域請求是不允許的。NS_ERROR_FAILURE:在Firefox中失敗
有沒有辦法解決這個問題,並讓JS運行在Chrome和Firefox?
請隨時索取您需要的額外細節!
這是我使用的代碼。
"use strict";
function showFixed(username)
{
console.log("Entered script");
var url = 'https://api-dev.bugzilla.mozilla.org/latest/bug'
+ '?quicksearch='
+ encodeURIComponent('FIXED @'+username);
displayBug(url);
}
function showPending(username)
{
console.log("Entered script");
var url = 'https://api-dev.bugzilla.mozilla.org/latest/bug'
+ '?quicksearch='
+ encodeURIComponent('@'+username);
displayBug(url);
}
function showCC(username)
{
console.log("Entered script");
var url = 'https://api-dev.bugzilla.mozilla.org/latest/bug'
+ '?quicksearch='
+ encodeURIComponent('cc:'+username);
displayBug(url);
}
function displayBug(url)
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET",url,false);
xmlhttp.send();
var text = xmlhttp.responseText;
var json = JSON.parse(text);
for(var i=0;i<json.bugs.length;i++)
{
var tempRow = document.createElement('tr');
var tempId = document.createElement('td');
tempId.innerHTML = '<a href=\'https://bugzilla.mozilla.org/show_bug.cgi?id=' + json.bugs[i].id + '\'>'+ json.bugs[i].id + '</a>';
var tempCreator = document.createElement('td');
tempCreator.innerHTML = json.bugs[i].creator.real_name;
var tempShortDesc = document.createElement('td');
tempShortDesc.innerHTML = json.bugs[i].summary;
var tempComponent = document.createElement('td');
tempComponent.innerHTML = json.bugs[i].component;
var tempAssignee = document.createElement('td');
tempAssignee.innerHTML = json.bugs[i].assigned_to.real_name;
var tempWhiteBoard = document.createElement('td');
tempWhiteBoard.innerHTML = json.bugs[i].whiteboard;
var tempBugStatus = document.createElement('td');
tempBugStatus.innerHTML = json.bugs[i].status;
var tempResolution = document.createElement('td');
tempResolution.innerHTML = json.bugs[i].resolution;
var tempLastChange = document.createElement('td');
tempLastChange.innerHTML = json.bugs[i].last_change_time;
tempRow.appendChild(tempId);
tempRow.appendChild(tempAssignee);
tempRow.appendChild(tempCreator);
tempRow.appendChild(tempBugStatus);
tempRow.appendChild(tempShortDesc);
tempRow.appendChild(tempLastChange);
document.getElementById('bugs-table-tbody').appendChild(tempRow);
}
document.getElementById('main').innerHTML = '';
}
function wrapper()
{
var waitString = "Please wait while bug list is loaded..."
document.getElementById('main').innerHTML = waitString;
你如何能夠運行XMLHttpRequest跨域?千萬不要在Chrome中爲我加載... – 2013-05-17 10:24:12
呃...我不知道它爲什麼會起作用,但它確實如此。我正在向bugzilla-api發送請求。如果你喜歡,你可以看看代碼。我在Chrome 26上進行了測試,也使用了老版本的鉻和鉻。 – ffledgling 2013-05-18 22:45:12
這很有趣,謝謝。 – 2013-05-19 05:44:12