3

There is a XMLHttpRequest in the content script of my Firefox WebExtensions add on. Q: why is the status of this request is always 0?

This is the JavaScript code making the request:

var query = "http://api.wolframalpha.com/v2/query?appid=[MY-APP-ID]&includepodid=Comparison&scanner=Unit&format=plaintext&input=1%20lm";
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function()
{
    console.log("onreadystatechange");
    console.log(this);
    if (this.readyState == 4 && this.status == 200)
    {
        onSuccess(this.responseText);
    }
};
xhttp.open("GET", query, true);
xhttp.send();

If I print out the results of the request for each onreadystatechange call, I get:

XMLHttpRequest { onreadystatechange: makeWolframRequest/xhttp.onreadystatechange(),
    readyState: 1, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload,
    responseURL: "", status: 0, statusText: "", responseType: "", response: "" }
XMLHttpRequest { onreadystatechange: makeWolframRequest/xhttp.onreadystatechange(),
    readyState: 2, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload,
    responseURL: "", status: 0, statusText: "", responseType: "", response: "" }
XMLHttpRequest { onreadystatechange: makeWolframRequest/xhttp.onreadystatechange(),
    readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload,
    responseURL: "", status: 0, statusText: "", responseType: "", response: "" }

Things I checked:

Makyen
  • 31,849
  • 12
  • 86
  • 121
Gladclef
  • 687
  • 1
  • 8
  • 17
  • The `readystatechange` event fires whenever the readystate changes, the first three happens before the server responds, and you can't have a status code before you have a response, that's why it's `0` the first three times, and that's also why we check that the `readystate` is `4`, as that indicates that a response was gotten. The issue likely isn't the status code for the three first `readystatechange` calls, but something else. – adeneo Nov 28 '16 at 07:13
  • From my experiments, the readystate value of logs is 1, 2, and then 4. I would agree a status of 0 makes sense for states 1 and 2 (in the first two logs), but the readystate of 4 combined with status 0 is why I'm concerned. – Gladclef Nov 28 '16 at 07:17
  • If the status is still `0` when the readystate is `4`, that usually indicates some other problem. I don't have an app ID and can't test this, but I'd guess it's a CORS error. Did you set the correct permissions for your script, you can't do cross origin calls without allowing it by asking for the right permissions? – adeneo Nov 28 '16 at 07:21
  • @adeneo If you would like, you can post an answer for me to choose since it was indeed a CORS issue. – Gladclef Nov 28 '16 at 07:35

1 Answers1

1

In this case it was a CORS issue. I had to add this secret sauce to my manifest.json file:

"permissions": [
    "http://api.wolframalpha.com/*"
]

More information here: https://developer.mozilla.org/en-US/Add-ons/WebExtensions/manifest.json/permissions

Much thanks to @adeneo for insisting I keep looking at CORS issues.

Gladclef
  • 687
  • 1
  • 8
  • 17
  • This issue would almost certainly produced some output in the [Browser Console](https://developer.mozilla.org/en-US/docs/Tools/Browser_Console) (Ctrl-Shift-J, or Cmd-Shift-J on OSX) when you executed this code which would have given you a good idea as to what the problem was. There are also [other consoles](http://stackoverflow.com/a/38920982/3773011) which you could have looked in to see information about this issue. – Makyen Nov 28 '16 at 07:48