【问题标题】:post data from chrome extension从 chrome 扩展发布数据
【发布时间】:2023-03-13 19:20:01
【问题描述】:

我有一个从用户接收数据的rest方法,这里是CURL命令:

curl -X POST -H "Content-Type: application/json" -d "data_test" http://localhost:8080/datum

现在我正在尝试POST 数据,使用 java 脚本:

var request = new XMLHttpRequest();
let url='http://localhost:8080/datum';
let data=body;
request.open('POST', url, true);
request.setRequestHeader("Content-Type", "application/json");
request.send(data);

但这什么也没收到。

我在 Chrome-Extension 中使用这个 java 脚本。

【问题讨论】:

  • 现代 Chrome 不允许内容脚本发出跨域请求。在您的后台脚本中执行此操作并使用消息传递official CORB explainer 中的结果、更多信息和示例。
  • 但是这个thread 说了别的,我不知道该怎么办.. @wOxxOm

标签: javascript curl post google-chrome-extension xmlhttprequest


【解决方案1】:

这是我当前的代码:

var xhr = new XMLHttpRequest();
        xhr.open("POST", "http://localhost:8080/datum", true);
        xhr.setRequestHeader("Content-type", "application/json");
        xhr.onreadystatechange = function() {
          if (xhr.readyState == 4) {
            console.log(xhr.responseText);
          }
        }
        xhr.send(dat);

有效,问题出在我的 rest api 上。

【讨论】: