开发者

Getting a Status Code of 0 and empty data when doing some XHR in a Background Page of a Chrome Extension

I am writing an extension (my first want, also I am not well versed in JS, actually this is my first project in the language). And I am having problems while fetching an url using XHR the backgroud page.

I am getting an status code of 0 and no data from a request. The strange thing is that when I use Wireshark (to sniff the packages sent by Chrome) I get the data OK and with a status code of 200. Below you see my manifest file and the code. I basically copied the code form the content script documentation page link:

http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/howto/contentscript_xhr/

I have read that it sometimes happens when you don't indicate in the manifest.json the permissions but I think they are ok. Thank you in advance.

Heres the manifest:

Manifest.json

{
   "name": "XXXXX",

"version": "0.1",
"description": "test extension",


"browser_action": {
"default_icon": "icon.png",
"popup": "popup.html"
 },
"content_scripts": [
{
  "matches": ["http://*.amazon.com/*"],
  "js": ["jquery.js", "content.js"]
}
],

"background_page": "background.html",
"permissions": [
 "tabs",
 "http://finance.yahoo.com/*",
 "http://*.amazon.com/"
 ]
}

background.html

function fetchCurrency(callback) {
   var invocation = new XMLHttpRequest();
   var url = 'http://download.finance.yahoo.com/d/quotes.csv?e=.csv&f=l1&s=USDCOP=X';

  invocation.onreadystatechange = function(data) {
     alert('Invocation Ready State: '  +invocation.readyState);
 开发者_StackOverflow中文版    if (invocation.readyState == 4) {
      alert('Invocation Status: ' + invocation.status); //shows  0!!
       if (invocation.status == 200 ) {
         var data = invocation.responseText;
         alert('The data es ' + data);
         callback(data);

       } else {
         callback(null);
       }
     }
   }
 invocation.open('GET', url, true);
 invocation.send();
 };

function onRequest(request, sender, callback) {
   if (request.action == 'getRate') {
     fetchCurrency(callback);
   }
 };

 // Wire up the listener.
 chrome.extension.onRequest.addListener(onRequest);


/*
  I then connect the content script as exemplified in the link I
posted. All is working OK but I am getting a weird 0 status code :'(
*/


Try this in your manifest instead:

"http://*.finance.yahoo.com/*"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜