简体   繁体   English

XMLHttpRequest 不发送 Firefox(插件)

[英]XMLHttpRequest not sending in Firefox (addon)

I'm developing an addon that needs to do a POST request to my server.我正在开发一个需要向我的服务器发出 POST 请求的插件。

I'm using XMLHttpRequest for this.我为此使用 XMLHttpRequest。 It's working great on Chrome, but the POST request is never sent on Firefox. Nothing is showing in the dev tools (console /.network / addon inspect console), and nothing is received on my server.它在 Chrome 上运行良好,但从未在 Firefox 上发送 POST 请求。开发工具(控制台/.network/插件检查控制台)中没有显示任何内容,我的服务器上也没有收到任何内容。 I've disabled any other addons and any kind of security on my whole PC (yes, I'm this desperate).我已经在我的整个 PC 上禁用了任何其他插件和任何类型的安全(是的,我很绝望)。

manifest.json清单.json

{
    ...
    "manifest_version": 2,
    "content_scripts": [{
        "matches": ["*://myurl.com/*"],
        "js": ["content.js"],
        "run_at": "document_end"
    }],
    "permissions": ["activeTab", "webRequest"]
}

The post request in my content script我的内容脚本中的发布请求

const post = () => {
    const xhr = new XMLHttpRequest();
    xhr.open('POST', 'https://myserver.com/test', false);
    xhr.setRequestHeader('Content-Type', 'application/json');
    console.log('Before sending request');
    xhr.send(JSON.stringify({id: myId}));
    console.log('Request sent');
    return xhr.responseText;
};

Only the "Before sending request" is logged.仅记录“发送请求前”。 Anything after the xhr.send() is never executed (this includes code after the post() call in the rest of my code). xhr.send() 之后的任何内容都不会执行(这包括我代码的 rest 中 post() 调用之后的代码)。

My server accepts all origins.我的服务器接受所有来源。 It's also correctly configured to received and process POST requests它还正确配置为接收和处理 POST 请求

const cors = require('cors')
app.use(cors());

What's wrong with this?这有什么问题吗? It's perfectly working on Chrome and I have no idea why it's never being sent without leaving any kind of error...它在 Chrome 上完美运行,我不知道为什么它永远不会在不留下任何错误的情况下发送......

Per the post on firefox XHR working differently based on calling context consider moving the call to a background script to see if you get different behaviour.根据firefox 上的帖子,XHR 根据调用上下文以不同方式工作,考虑将调用移至后台脚本以查看您是否获得不同的行为。

You should consider adding your URL (eg "*://myurl.com/*" ) as a host permission to the permission array to have privilege for XMLHttpRequest. Please see official doc here in MDN and Chrome docs .您应该考虑将您的 URL(例如"*://myurl.com/*" )作为主机权限添加到权限数组中,以获得 XMLHttpRequest 的权限。请参阅MDNChrome 文档中的官方文档

You can see this issue was also covered in this SO answer as well您可以看到这个问题也包含在这个SO answer

BTW, I would consider migrating to manifest V3 following this Chrome suggestion:顺便说一句,我会考虑按照 Chrome 的建议迁移到 manifest V3:

The Chrome Web Store no longer accepts Manifest V2 extensions. Chrome Web 商店不再接受 Manifest V2 扩展。 Please use Manifest V3 when building new extensions.构建新扩展时请使用 Manifest V3。

As @Cadmium suggested in the question comments (and even though I didn't want to handle this request there), I've moved my post() function to a background script.正如@Cadmium 在问题评论中建议的那样(即使我不想在那里处理此请求),我已将我post() function 移至后台脚本。

It's now working on both Chrome and Firefox. I still don't understand why it's behaving this way on Firefox, but it's a good enough workaround for me for now !它现在可以在 Chrome 和 Firefox 上运行。我仍然不明白为什么它在 Firefox 上会这样,但现在对我来说这是一个足够好的解决方法!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM