XMLHttpRequest在Firefox中不发送(插件)

huangapple go评论80阅读模式
英文:

XMLHttpRequest not sending in Firefox (addon)

问题

我正在开发一个需要向我的服务器发送POST请求的插件。

我在这里使用XMLHttpRequest。在Chrome上运行得很好,但是在Firefox上POST请求从未发送过。在开发工具(控制台/网络/插件检查控制台)中没有显示任何内容,也没有在我的服务器上接收到任何内容。我已经禁用了所有其他插件以及我的整个PC上的任何安全设置(是的,我非常绝望)。

manifest.json

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

内容脚本中的POST请求

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;
};

仅记录“Before sending request”。在xhr.send()之后的任何内容都不会执行(包括在我的其余代码中对post()调用之后的代码)。

我的服务器接受所有来源。它也已正确配置以接收和处理POST请求

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

这有什么问题吗?在Chrome上运行得很好,我不知道为什么在不留下任何错误的情况下从未发送过。

英文:

I'm developing an addon that needs to do a POST request to my server.

I'm using XMLHttpRequest for this. 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. I've disabled any other addons and any kind of security on my whole PC (yes, I'm this desperate).

manifest.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).

My server accepts all origins. It's also correctly configured to received and process POST requests

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...

答案1

得分: 1

根据关于火狐浏览器根据调用上下文不同而工作方式不同的帖子,考虑将调用移到后台脚本以查看是否获得不同的行为。

英文:

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.

答案2

得分: 0

正如 @Cadmium 在问题评论中建议的(尽管我不想处理这个请求),我已将我的 post() 函数移到后台脚本中。

现在它在Chrome和Firefox上都可以正常工作。我仍然不明白为什么它在Firefox上表现出这种方式,但现在对我来说已经足够好了!

英文:

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.

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 !

答案3

得分: -1

你应该考虑将您的URL(例如"*://myurl.com/*")添加为主机权限到权限数组中,以便在XMLHttpRequest中拥有特权。请在MDNChrome文档中查看官方文档。

您也可以在这个SO回答中找到关于这个问题的讨论。

顺便说一下,我建议您考虑根据Chrome的建议迁移到Manifest V3:

Chrome Web Store不再接受Manifest V2扩展。请在构建新扩展时使用Manifest V3。

英文:

You should consider adding your URL (e.g. "*://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.

You can see this issue was also covered in this SO answer as well

BTW, I would consider migrating to manifest V3 following this Chrome suggestion:

> The Chrome Web Store no longer accepts Manifest V2 extensions. Please use Manifest V3 when building new extensions.

huangapple
  • 本文由 发表于 2023年1月9日 04:16:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050976.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定