无法从 Chrome 扩展程序连接到本地主机

发布于 2024-12-08 09:58:41 字数 447 浏览 1 评论 0原文

我正在开发一个跟踪时间的 Chrome 扩展,并使用 Google App Engine 作为后端。

为了进行测试,我尝试将扩展程序的本地版本连接到 App Engine 应用程序的本地版本。当我尝试发送 POST 请求时,我收到:

XMLHttpRequest 无法加载 http://localhost:8080/report。 Access-Control-Allow-Origin 不允许 Origin chrome-extension://mbndmimplohfkkcincjodnfpaapbbmei。

但当我更改 URL 以便将其发布到 appspot.com URL 时,它会起作用。

什么是 Access-Control-Allow-Origin,为什么它阻止我从本地主机获取结果?

I am working on a Chrome extension that tracks time, and uses Google App Engine for the backend.

For testing, I'm trying to connect a local version of the extension to a local version of the App Engine app. When I try to send a POST request, I'm getting:

XMLHttpRequest cannot load http://localhost:8080/report. Origin chrome-extension://mbndmimplohfkkcincjodnfpaapbbmei is not allowed by Access-Control-Allow-Origin.

But it works when I change the URL so that it posts to the appspot.com URL.

What is the Access-Control-Allow-Origin, and why is it stopping me from getting results from localhost?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

慈悲佛祖 2024-12-15 09:58:41

我相信这是因为您无法调用未包含在清单的权限部分中的服务器。 manifest.json 的权限部分应如下所示:

"permissions": [
  "http://myapp.appspot.com/*",
  "http://localhost/*"
]

注意,我尚未对此进行测试,但听起来这就是您的问题的根源。

I believe this is because you cannot make calls to a server that is not included in the permissions section of your manifest. The permissions section of manifest.json should look something like this:

"permissions": [
  "http://myapp.appspot.com/*",
  "http://localhost/*"
]

Note, I haven't tested this, but it sounds like that is where your problem is coming from.

花伊自在美 2024-12-15 09:58:41

可以使用自定义端口。

ma​​nifest.json

"permissions": ["http://localhost/*"]

background.js (使用 jQuery)

$.post('http://localhost:5000/some-endpoint');

You can use custom ports.

manifest.json

"permissions": ["http://localhost/*"]

background.js (using jQuery)

$.post('http://localhost:5000/some-endpoint');
千寻… 2024-12-15 09:58:41

我能够让这段代码工作:

var loginPayload = {};
loginPayload.username = document.getElementById('username').value;
loginPayload.password = document.getElementById('password').value;
console.log(loginPayload);

var callback = function (response) {
    console.log(response);
};
var handle_error = function (obj, error_text_status){
    console.log(error_text_status + " " + obj);
};

$.ajax({
    url: 'https://127.0.0.1:8443/hello',
    type: 'POST',
    success: callback,
    data: JSON.stringify(loginPayload),
    contentType: 'application/json',
    error: handle_error
});

编辑:
显然有人不喜欢这个,所以要记住一些事情:

  1. 对于必须在 https 上运行的扩展,请确保您的服务器提供 https 服务。
  2. 与上面的帖子相反,chrome 扩展可以在端口 80 / 443 以外的端口上提供服务

I was able to get this code to work:

var loginPayload = {};
loginPayload.username = document.getElementById('username').value;
loginPayload.password = document.getElementById('password').value;
console.log(loginPayload);

var callback = function (response) {
    console.log(response);
};
var handle_error = function (obj, error_text_status){
    console.log(error_text_status + " " + obj);
};

$.ajax({
    url: 'https://127.0.0.1:8443/hello',
    type: 'POST',
    success: callback,
    data: JSON.stringify(loginPayload),
    contentType: 'application/json',
    error: handle_error
});

EDIT:
Apparently someone didn't like this, so a few things to keep in mind:

  1. For extensions that have to work on https, make sure your server is serving https.
  2. Contrary to posts above, chrome extensions CAN serve on ports other than port 80 / 443
想你的星星会说话 2024-12-15 09:58:41

如果您想要通用的本地主机方法

*://localhost:*/*

if you want a generic localhost approach

*://localhost:*/*
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文