无法使http请求javascript ns错误失败 [英] cannot make http request javascript ns error failure
问题描述
我使用了一个提供JSON数据值的网站。
http://dazzlepod.com/ip/stackoverflow.com.json
现在问题出在我做XMLHttp请求获取JSON值它显示 0x80004005(NS_ERROR_FAILURE)
消息:[例外...Failurensresult:0x80004005
(NS_ERROR_FAILURE)location:.... ...... :: httpGet :: line 30
data:no]
这里是我的ScriptCode
函数getDomainInfo url){
var a = document.createElement('a');
a.href = url;
var domain = a.hostname;
var requestURL =http://dazzlepod.com/ip/\"+domain+\".json;
// alert(requestURL);
return httpGet(requestURL);
}
函数httpGet(theUrl)
{
if(window.XMLHttpRequest)
{//代码为IE7 + ,Firefox,Chrome,Opera,Safari
xmlhttp = new XMLHttpRequest();
}
else
{//代码为IE6,IE5
xmlhttp = new ActiveXObject(Microsoft.XMLHTTP);
xmlhttp.onreadystatechange = function()
{
if(xmlhttp.readyState == 4&& xmlhttp.status == 200)
{
return xmlhttp.responseText;
xmlhttp.open(GET,theUrl,false);
xmlhttp.send();
var check = self.port.on(clicked,function(url){
var json = getDomainInfo(url);
// alert (json);
});
任何人都可以解释为什么会发生这种情况,应该怎么办?
I '
谢谢。
GitHUb链接 https://github.com/kannojia/show-ip
好的,我解决了这个问题。问题在于,由于同源策略,您无法将XMLHttp请求放在其他域中。
因此,当ContentScript向服务器发出XMLHttp请求时,浏览器会抛出 NS_error_failure
。
修改的代码 main.js :
var widgets = require(sdk /窗口小部件);
var tabs = require(sdk / tabs);
var data = require(sdk / self)。data;
var Request = require(sdk / request)。
var url;
var domain_name;
var requestURL;
$ b $ showipWidget = widgets.Widget(
id:show-ip,
label:显示当前页面的IP地址,
contentURL:data .url(lens_icon.png),
contentScriptFile:[data.url(click_handler.js),data.url(jquery-1.8.3.min.js)],
面板:infoPanel,
onClick:function(){
var curtab = tabs.activeTab;
url = curtab.url;
this.port.emit('getDomain',url );
}
});
showipWidget.port.on(setDomain,function(domain){
domain_name = domain;
getDomainInfo(domain_name);
});
函数getDomainInfo(domain){
requestURL =http://dazzlepod.com/ip/\"+domain+\".json;
//requestDomainInfo.get();
请求({
url:requestURL,
onComplete:function(response){
var out = response.json;
console.log(回应:+ out.ip);
}
})。get();内容脚本 click_handler.js
$ b <
$ b function getDomain(url){
var a = document.createElement('a');
a.href = url;
var domain = a.hostname;
返回域;
var check = self.port.on(getDomain,function(url){
var domain = getDomain(url);
self.port .emit(setDomain,domain);
});
所有文件位于GitHub上
谢谢大家的帮助。 / p>
I started learning addon development using addon-sdk. I'm writing a small addon script which display IP address of current webpage.
I used a website which provides JSON data values.
http://dazzlepod.com/ip/stackoverflow.com.json
Now the problem is when I make XMLHttp request to get JSON values it shows 0x80004005 (NS_ERROR_FAILURE)
Message: [Exception... "Failure" nsresult: "0x80004005
(NS_ERROR_FAILURE)" location: .... ...... :: httpGet :: line 30"
data: no]
Here's my ScriptCode
function getDomainInfo(url) {
var a = document.createElement('a');
a.href = url;
var domain = a.hostname;
var requestURL = "http://dazzlepod.com/ip/"+domain+".json";
//alert(requestURL);
return httpGet(requestURL);
}
function httpGet(theUrl)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
return xmlhttp.responseText;
}
}
xmlhttp.open("GET", theUrl, false );
xmlhttp.send();
}
var check = self.port.on("clicked", function(url) {
var json = getDomainInfo(url);
//alert(json);
});
Can Anyone explain why this is happening and What should I do?
I'm kind of new to addon development.
Thank You.
GitHUb Link https://github.com/kannojia/show-ip
解决方案 Ok, I solved the problem. The problem was that you cannot make XMLHttp request to some other domain while residing on some other due to same origin policy .
Therefore when contentScript made XMLHttp request to the server, the browser throws NS_error_failure
.
The solution I found is to use Request API of Addon SDK . The Request object is used to make GET, POST, or PUT network requests. The Request object is not subjected to same-origin-policy.
So instead of making Http request from contentScript, I used request API and got the valid response.
The modified code main.js :
var widgets = require("sdk/widget");
var tabs = require("sdk/tabs");
var data = require("sdk/self").data;
var Request = require("sdk/request").Request;
var url;
var domain_name;
var requestURL;
var showipWidget = widgets.Widget({
id : "show-ip",
label : "Display IP Address of current Page",
contentURL : data.url("lens_icon.png"),
contentScriptFile : [data.url("click_handler.js"),data.url("jquery-1.8.3.min.js")],
panel : infoPanel,
onClick : function() {
var curtab = tabs.activeTab;
url = curtab.url;
this.port.emit('getDomain',url);
}
});
showipWidget.port.on("setDomain", function(domain) {
domain_name=domain;
getDomainInfo(domain_name);
});
function getDomainInfo(domain) {
requestURL = "http://dazzlepod.com/ip/"+domain+".json";
//requestDomainInfo.get();
Request({
url: requestURL,
onComplete: function (response) {
var out = response.json;
console.log("Response: "+out.ip);
}
}).get();
}
And the Content Script click_handler.js
function getDomain(url) {
var a = document.createElement('a');
a.href = url;
var domain = a.hostname;
return domain;
}
var check = self.port.on("getDomain", function(url) {
var domain = getDomain(url);
self.port.emit("setDomain",domain);
});
All files are on GitHub.
Thank You Everyone for your help.
这篇关于无法使http请求javascript ns错误失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!