Chrome extension xhr request getting cancelled

筅森魡賤 提交于 2019-12-24 02:18:20

问题


I am trying to build a chrome extension which uses a xhr request to get response from a external api. I have set the permission for the extension as mention in the chrome extension document still xhr request is getting cancelled in network.

manifest.json

  "browser_action": {
      "default_icon": "icon.png",
      "default_popup": "popup.html",
      "default_title": "Chrome extension title"
  },
 "permissions": [
     "activeTab",
     "storage",
     "https://*/"
 ]

In 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:


I missed to add event.preventDefault(), that used to cancel the default form submit. When i added event.preventDefault() code to the above code it worked.



来源:https://stackoverflow.com/questions/46325437/chrome-extension-xhr-request-getting-cancelled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!