【发布时间】:2017-09-24 18:33:47
【问题描述】:
我正在尝试构建一个 chrome 扩展,它使用 xhr 请求从外部 api 获取响应。我已经设置了扩展的权限,正如 chrome 扩展文档中提到的那样,xhr 请求仍在网络中被取消。
manifest.json
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html",
"default_title": "Chrome extension title"
},
"permissions": [
"activeTab",
"storage",
"https://*/"
]
在 popup.js 中
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://putsreq.com/4z01VNOBPeD144njWNdi", true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && this.status == 200) {
var theValue = "asdfassf";
alert("This is doen");
// chrome.storage.sync.set({'value': theValue}, ()=> {
// // Notify that we saved.
// document.location.href = "timer.html";
// });
}
}
xhr.send();
【问题讨论】:
-
1.在 chrome://extensions 页面 2 上重新加载扩展。右键单击弹出窗口
Inspect,检查控制台是否有错误。 -
是的,我确实重新加载了很多次。在控制台的网络选项卡中,请求显示已取消
-
有其他干扰。贴出的代码是正确的。
-
@wOxxOm 你有调用外部脚本的工作应用吗?
标签: google-chrome google-chrome-extension permissions xmlhttprequest