Chrome扩展程序xhr请求被取消 [英] Chrome extension xhr request getting cancelled

查看:1248
本文介绍了Chrome扩展程序xhr请求被取消的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个使用xhr请求从外部api获取响应的chrome扩展。我已经在扩展文件中设置了扩展的权限,但xhr请求正在网络中被取消。

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

manifest.json

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



在popup.js中

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();


推荐答案

我错过了添加event.preventDefault()用于取消默认表单提交。当我将event.preventDefault()代码添加到上面的代码中时它就起作用了。

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.

这篇关于Chrome扩展程序xhr请求被取消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆