Chrome extension xhr request getting cancelled

2019-08-01 03:50发布

问题:

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.