我正在尝试构建一个 chrome 扩展,它使用 xhr 请求从外部 api 获取响应。我已经设置了扩展的权限,如 chrome 扩展文档中所述,但 xhr 请求在网络中被取消。
清单.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();
我错过了添加 event.preventDefault(),它用于取消默认表单提交。当我将 event.preventDefault() 代码添加到上面的代码中时,它起作用了。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)