0
我正在写一个扩展(我的第一个想要的,也是我不熟悉的JS,实际上这是我的第一个项目在语言中)。而我 在使用XHR背景页面获取URL时遇到问题。在Chrome扩展的后台页面中执行一些XHR时获取状态码0和空数据
我得到的状态码是0,没有来自请求的数据。 奇怪的是,当我使用Wireshark(嗅探由Chrome发送的包 )时,我得到的数据OK并且状态码为200.在 以下,您会看到我的清单文件和代码。我基本上是复制的代码 形式的内容脚本文件页面链接:
我已阅读,有时会发生,当你没有在manifest.json中的权限说明,但我觉得他们都OK。先谢谢你。
继承人的清单:
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);
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 :'(
*/
谢谢。有效!!! :-D。我认为它必须是确切的主人。这不是一个铬错误?或者我误解了整件事情? – mfcabrera 2011-05-12 14:56:53
你有什么工作,但'download.finance.yahoo.com'算作差异。域。 – mattsven 2011-05-12 15:36:39
是的...这是一个愚蠢的错误...我只是意识到它是下载.finance.yahoo.com不finance.yahoo.com X - D。谢谢:D。 – mfcabrera 2011-05-12 16:05:40