2013-03-15 712 views
16

我正在使用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; 
+0

你如何能够运行XMLHttpRequest跨域?千万不要在Chrome中为我加载... – 2013-05-17 10:24:12

+0

呃...我不知道它为什么会起作用,但它确实如此。我正在向bugzilla-api发送请求。如果你喜欢,你可以看看代码。我在Chrome 26上进行了测试,也使用了老版本的铬和铬。 – ffledgling 2013-05-18 22:45:12

+0

这很有趣,谢谢。 – 2013-05-19 05:44:12

回答

2

如果你能够使用jQuery,我建议在看看JSONP(http://www.jquery4u.com/json/jsonp-examples/)这有效地允许跨域AJAX。

+1

我希望能够限制它纯JS,没有使用任何其他的库等,但我会给这个镜头。 – ffledgling 2013-06-07 23:34:06

+2

@ffledgling这个人用jQuery回答了一个JavaScript问题已经够糟糕了,更糟糕的是,你放弃寻找最简单的路线,而不是花费时间找出正确的答案,而不会迫使另一个70KB的带宽被浪费。 – John 2014-07-06 21:50:45

+8

@John我没有放弃寻找答案,我尝试了所有其他正确的JS方法,然后才问这个问题,所以我仍然不知道用什么“正确”的方法来做到这一点是使用纯JS。不要批评真正试图回答问题的人,如果你知道一种方法来做到这一点,你应该继续并添加你的答案。 – ffledgling 2014-07-07 18:44:58