[英]Chrome extension XHR to localhost never arrives?
I am trying to write a chrome extension, and this is the first time I have needed to POST to a server with one. 我正在尝试编写chrome扩展程序,这是我第一次需要使用POST扩展到服务器。 If I fire a request in Postman to my local server at
https://localhost:80/savelink
I get a 200 and the server behaves accordingly. 如果我在Postman中将请求发送到我的本地服务器,
https://localhost:80/savelink
为https://localhost:80/savelink
,则得到200,服务器将相应地运行。 However, when I try to fire a request from the chrome extension, the server behavior is never triggered. 但是,当我尝试从chrome扩展名触发请求时,服务器行为永远不会触发。 The request is as follows (only slightly modified from the example):
请求如下(仅对示例稍作修改):
chrome.tabs.query(queryInfo, function(tabs) {
var tab = tabs[0];
var url = tab.url;
console.assert(typeof url == 'string', 'tab.url should be a string');
var request = new XMLHttpRequest();
request.open("POST", 'https://localhost:80/savelink', true);
request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
request.send({ tab_url: url });
console.log("HIT");
});
My manifest.json permissions look like this (can you tell I am pulling my hair?): 我的manifest.json权限如下所示(您能告诉我要拔头发吗?):
"permissions": [
"activeTab",
"https://localhost/*",
"https://*/",
"http://*/"
]
and relevant parts of my server look like: 服务器的相关部分如下所示:
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.post('/savelink', function(req, res) {
console.log(req.body); // this log is never triggered when the route is hit through chrome
res.sendStatus(200);
});
I am running Chrome Canary with the --disable-web-security flag set. 我正在运行带有--disable-web-security标志设置的Chrome Canary。 I am still getting an insecure response on the client side, but I think that is probably irrelevant.
我仍然在客户端收到不安全的回复,但是我认为这可能是不相关的。
Okay, I figured it out, thank you to the commenters, you were all pretty much on the right path. 好的,我想通了,谢谢评论者,你们几乎都在正确的道路上。 I did have to run
open -a "Google Chrome Canary" --args --user-data-dir --disable-web-security
, but on top of that, any change made to the server relaunches it (which I blanked on). 我确实必须运行
open -a "Google Chrome Canary" --args --user-data-dir --disable-web-security
,但是最重要的是,对服务器所做的任何更改都会重新启动它(我对此进行了说明) )。 That means that in order to get my calls to work, I need to navigate to a route on my server, load it, accept the warning, and then open my extension and send my POST
. 这意味着要开始工作,我需要导航到服务器上的路由,加载它,接受警告, 然后打开我的扩展程序并发送
POST
。 Change the extension means I have to do that + reload the extension. 更改扩展名意味着我必须这样做+重新加载扩展名。 Sorry to everyone I responded to, y'all were right, I missed some steps.
对不起,我回答的每个人都正确,我错过了一些步骤。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.