【发布时间】:2013-03-04 17:40:42
【问题描述】:
我正在使用 javascript 的 XMLHttpRequest 对象向另一个页面发送请求(不在同一服务器或域名上)我在 firefox 中收到 ns_error_failure 错误,但 Javascript 在 Google Chrome 中工作,在网上搜索后似乎是因为Firefox 的 XSS 策略。不允许跨域请求。
有没有办法解决这个问题并让 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 中为我加载...
-
嗯......我不知道它为什么有效,但它确实有效。我正在向 bugzilla-api 发送请求。如果您愿意,可以查看代码。我在 Chrome 26 上对其进行了测试,也适用于较旧的 chrome 和 chromiums。
-
那会很有趣,谢谢。
-
@Qantas94Heavy 查看编辑。您可能只想查看其中一个 show* 函数和 displaybug 函数的初始位
-
你试过
xmlhttprequest.send( null )而不是xmlhttprequest.send( )吗?我记得我曾经因此而收到此错误消息。
标签: javascript ajax firefox xmlhttprequest xss