Cross-Origin XMLHttpRequest from Chrome Extension Returning Status 0

932 views Asked by At

I am writing a Chrome Extension which needs to load some data from another site. From my research I believe I should use XMLHttpRequest for this but it returns responseText="" and status=0 for every site that I try. The URLs that I pass into the javascript are good; I have tried

http://www.google.com

and

http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22MSFT%22)&env=store://datatables.org/alltableswithkeys.

Am I doing something ignorant/stupid? I believe that it is something with the permissions in the manifest, but what I have now should allow both of these sites. I see no errors in my javascript console.

The manifest:

{
  "manifest_version": 2,

  "name": "nnnn",
  "description": "nnnn",
  "version": "1.0",

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "permissions": [
    "storage",
    "http://*.query.yahooapis.com/*",
    "http://*/*",
    "https://*/*"    
  ]
}

The javascript function:

function getSite(queryUrl) {
  var request = new XMLHttpRequest();
  request.open("GET", queryUrl, true);
  console.log(queryUrl);
  request.onreadystatechange = function () {
    console.log(request);
   if (request.readyState == 4) {
      if (request.status == 200) {
        console.log(request.responseText);
      } else {
        console.log('Unable to resolve address');
      }
    }
  };
  request.send(null);
}

EDIT: http://developer.chrome.com/extensions/xhr.html This is pretty much what I have been following for guidance but apparently something has gone over my head.

2

There are 2 answers

0
user64208 On BEST ANSWER

I did something ignorant and had a form submitting which was causing the page to refresh and throw away the previous XMLHttpRequest when it would finish

3
Uzair Farooq On

I don't believe it's a permission related problem. If it were, there would be an error message in console announcing permission problem. I think there's something wrong with your XMLHttpRequest. Make sure the queryUrl you pass to the function is correct.

You can use Fiddler to check out what's wrong with your request.