0

I am making a Firefox addon, and from a Firefox addon panel, I am trying to make an AJAX request to a site.

This is what I have so far:

package.json

{
  //[...]
  "permissions": {
    "cross-domain-content": ["http://www.website.com", "https://www.website.com"]
  }
}

main.js

var data = require("sdk/self").data;

var extension_panel = require("sdk/panel").Panel({
  contentURL: data.url("menu.html"),
  contentScriptFile: data.url("menu.js")
});
//[...]

menu.js

//[...]
$.ajax({
    type: "GET",
    url: "https://www.website.com/currentUser",
    success: function(e){
        $("#loading").hide();
    },
    error: function(e){
        $("#loading").hide();
        if (e.status == 401){
            show_login();
            return;
        }
        show_login();
        $("#login-error").text("Couldn't check login status!").show();
    }
});
//[...]

Every time I run this addon, it keeps giving the Couldn't check login status! message. When I log the error object, the status is 404, and statusText is "error".

I think I have everything properly set up in package.json, so why is the addon not allowing me to make the AJAX request?

EDIT

404 in this case doesn't mean that the URL doesn't exist. In the Chrome version of this very extension, everything works fine, so I know that the website hasn't changed on a last-minute basis.

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Ivan
  • 307
  • 1
  • 16
  • @dandavis Yes, but in the Chrome version of this very addon, the URL works just fine. – Ivan Oct 25 '14 at 23:00
  • might need additional headers also. Check API docs – charlietfl Oct 25 '14 at 23:03
  • @charliefl What headers? The website is made to return a `200` when the user is logged in, and a `401` for every other request. – Ivan Oct 25 '14 at 23:05
  • It looks like you're using jquery. So the ajax is coming not off of the "background" page (google chrome ext dev lingo) but its coming from within the page itself, which of course doesnt allow cross domain xhr. you'll have to xhr from the non-contentScript scope, this should work. im not familiar with addon sdk so im not sure how `cross-domain-content` in package.json works but i added sdk tag that should get you better replies – Noitidart Oct 25 '14 at 23:26
  • @dandavis I already did the debugging. Nothing useful showed up. – Ivan Oct 25 '14 at 23:31
  • @Noitidart The AJAX didn't come from Chrome's background page either. It was directly in the popup. As far as to doing the XHR from the non-contentScript scope, I've considered that, but I want to solve this puzzle first. – Ivan Oct 25 '14 at 23:32
  • Let's wait for the sdk experts to answer that. However if you want to do non-contentScript scope here's the function to do it with: http://stackoverflow.com/questions/25109620/how-to-read-html-content-of-a-specific-url-using-firefox-addon/25112976#25112976 just replace `var {Cu: utils, Cc: classes, Ci: instances} = Components;` with `const { Ci, Cu, Cc, Cr } = require('chrome');` – Noitidart Oct 25 '14 at 23:35
  • @Noitidart I already know how to do Addon SDK's XHR, but thanks anyway. – Ivan Oct 25 '14 at 23:36
  • it's just strange that the website is supposed to return a 401 or 200 and you get a 404. if the call was blocked, you wouldn't get a 404, you would get a zero... that's why i think it's something simple. – dandavis Oct 25 '14 at 23:38
  • @dandavis I'll look into it some more in a few minutes. Maybe it's the way Firefox does things, I don't really know. – Ivan Oct 25 '14 at 23:47
  • A plain vanilla `XMLHttpRequest` produces the same result? jQuery is included with a `script` tag? – paa Oct 26 '14 at 18:11
  • @paa I actually did some further testing, and learned that if I run the extension in the addon SDK via `cfx run`, it doesn't work, but if I package the extension into an .xpi file, it starts working again. – Ivan Oct 26 '14 at 19:19

1 Answers1

1

From what I learned, it seems to be a bug in the Addon SDK.

Debugging the extension with cfx run disables XHR for some reason, so if I were to run install the packaged .xpi extension, XHR (and everything else) works again.

Ivan
  • 307
  • 1
  • 16
  • For a workaround, please see [this answer](http://stackoverflow.com/questions/10519440/how-to-modify-source-code-without-re-running-cfx-and-firefox-when-debugging-an-s/10519963#10519963) – therealjeffg Oct 27 '14 at 17:39