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.