首页 > 解决方案 > XMLHttpRequest 在 chrome 上工作,但在 firefox 上不工作

问题描述

我目前正在开发一个扩展以在我的工作中集成两个系统,chrome 扩展运行得很好并且运行良好,但是我的团队还要求一个 firefox 版本,在 firefox 中我遇到了 xmlhttprequest() 的问题,

调用请求的函数是这个:

function getModelList(API_KEY)
{
  return new Promise(resolve => {
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open( "POST", 'http://10.255.12.128/api/get_models/', true );
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.onload = function(e) {
      resolve(xmlHttp.response);
    };
    xmlHttp.onerror = function (e) {
      resolve(undefined);
      console.error("** An error occurred during the XMLHttpRequest");

    };
    xmlHttp.send( 'API_KEY='+API_KEY );
  })
}

(我知道在 POST 中发送 API_KEY 是不安全的,但它只在我们的本地网络中运行,所以现在可能没问题)

当我运行它时,它直接进入onerror,它比超时更快,并且在检查时不显示在网络选项卡中,我想我需要在firefox中给它一些权限或其他东西才能运行?

另外,扩展的清单是这个(也许问题在这里?):

{
    "name" : "Zabbix-Bitrix Integration",
    "version": "0.0.4",
    "manifest_version": 2,
    "description" : "Insere funções extras ao zabbix",
    
    "options_ui": {
        "page": "options.html",
        "open_in_tab": false,
        "browser_style": true,
        "chrome_style": true
    },

    "content_scripts" : [
        {
            "js" : ["init.js"],
            "css": ["styles.css"],
            "matches" : ["*://zabbix.monitor.redeunifique.com.br/zabbix.php?action=problem.view*"]
        }
    ],
    "permissions": ["storage","webRequest"]
}

@edit,经过一番挖掘,发现一条错误消息:错误:收到请求http://10.255.12.128/api/get_models/没有设置浏览上下文ID

调用错误的函数有这样的注释:

// Ensure that we have a browsing context ID for all requests when debugging a tab (=`browserId` is defined).
// Only privileged requests debugged via the Browser Toolbox (=`browserId` null) can be unrelated to any browsing context.
if (!this._browsingContextID && this._networkEventWatcher.browserId) {
  throw new Error(
    `Got a request ${this._request.url} without a browsingContextID set`
  );
}

如何以及在哪里设置此上下文 ID?

标签: javascriptapifirefoxxmlhttprequest

解决方案


推荐阅读