ajax NS_ERROR_FAILURE : Firefox 失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15442046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 10:58:07  来源:igfitidea点击:

NS_ERROR_FAILURE : Failure in Firefox

javascriptajaxfirefoxxmlhttprequestxss

提问by ffledgling

I'm using javascript's XMLHttpRequest object to send a request to another page (not on the same server or domainname ) I get a ns_error_failure error in firefox, but the Javascript works in Google Chrome, after searching online it seems to be because of firefox's XSS policy. Cross-Domain requests are not allowed.

我正在使用 javascript 的 XMLHttpRequest 对象将请求发送到另一个页面(不在同一服务器或域名上)我在 firefox 中收到 ns_error_failure 错误,但 Javascript 在 Google Chrome 中工作,在网上搜索后似乎是因为 firefox 的 XSS政策。不允许跨域请求。

Is there anyway to work around this and make the JS run in both chrome and Firefox?

有没有办法解决这个问题并使 JS 在 chrome 和 Firefox 中运行?



Please feel free to ask for additional details you feel are needed!

请随时询问您认为需要的其他详细信息!



Here's the code that I was using.

这是我使用的代码。

"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;

回答by NullHappens

If you are able to use jQuery, I would suggest having a look at JSONP (http://www.jquery4u.com/json/jsonp-examples/) this effectively allows crossdomain ajax.

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