从 Chrome 扩展程序中发布数据

3

我有一个Rest方法用于接收用户数据,下面是CURL命令:

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

现在我正在尝试使用JavaScript进行POST数据:

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扩展中使用这个javascript。

1
现代Chrome浏览器不允许内容脚本进行跨域请求。您可以在后台脚本中执行此操作,并使用消息传递来传输结果。更多信息和示例请参见官方CORB说明文档 - wOxxOm
但是这个线程说了另外一件事,我不确定该怎么做... @wOxxOm - Maifee Ul Asad
1个回答

2

这是我的当前代码:

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上。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接