Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过chrome扩展向REST发送数据_Javascript_Rest_Google Chrome_Google Chrome Extension - Fatal编程技术网

Javascript 通过chrome扩展向REST发送数据

Javascript 通过chrome扩展向REST发送数据,javascript,rest,google-chrome,google-chrome-extension,Javascript,Rest,Google Chrome,Google Chrome Extension,我尝试使用post将我的选择发送到服务器,服务器需要REST,使用高级REST客户端测试了我的数据,所有这些都很好,但是当我尝试使用chrome扩展时,我遇到了未定义错误。这就是我正在尝试的 chrome.runtime.sendMessage({ method: 'POST', action: 'REST', headers: {'Remote-User':'myuser'}, url: 'http

我尝试使用post将我的选择发送到服务器,服务器需要REST,使用高级REST客户端测试了我的数据,所有这些都很好,但是当我尝试使用chrome扩展时,我遇到了未定义错误。这就是我正在尝试的

chrome.runtime.sendMessage({
            method: 'POST',
            action: 'REST',
            headers: {'Remote-User':'myuser'},
            url: 'http://myserver/data/add/',
            data: {'content': 'the paste content'}
        }, function(responseText) {
            alert(responseText);
        });
非常感谢您的帮助,谢谢

更新

基于sdc,我这样更改了代码,但responseText未定义:(


我认为您误解了chrome.runtime.sendMessage的用途。请参阅文档的前两段。
sendMessage
用于“扩展及其应用程序之间的通信”,而不是用于发送HTTP请求。

内容脚本还必须“设置runtime.onMessage事件侦听器以处理消息”,只有这样,您才能从
sendMessage
请求收到有效响应

他们示例的结果是
undefined

chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
   console.log(response.farewell);
});
<- undefined

我认为您误解了chrome.runtime.sendMessage的用途。请参阅文档的前两段。
sendMessage
用于“扩展及其应用程序之间的通信”,而不是用于发送HTTP请求。

内容脚本还必须“设置runtime.onMessage事件侦听器以处理消息”,只有这样,您才能从
sendMessage
请求收到有效响应

他们示例的结果是
undefined

chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
   console.log(response.farewell);
});
<- undefined

我没有看到任何异常,只是“未定义”你在任何地方触发了你的
genericOnClick
方法吗?我是个十足的白痴,我没有在清单上添加权限..但是现在一切都好了,非常感谢sdcGlad我能帮上忙,很抱歉我没有问这个问题,你能发布你的例外吗?我没有看到任何例外,只是“未定义”你在任何地方触发了你的
genericOnClick
方法吗?我是个十足的白痴,我没有在清单上添加权限..但是现在一切都好了,非常感谢sdcGlad我能帮上忙,很抱歉我没有问到这一点,你也可以使用chrome来测试REST服务EAH,或者
$。ajax
和JQuery可以帮你更新d我的代码有更改,但仍然没有成功的想法告诉我你得到了什么,当使用XHR时,记得在清单中设置。另请参见:你也可以使用chrome来测试REST服务EAH或
$。ajax
和JQuery应该为你做这件事,用更改更新了我的代码,但仍然没有成功的想法告诉我你得到了什么,当使用XHR时,请记住设置清单中的ber。另请参阅:
var data = $.toJSON({'content': 'the paste content'});

var xhr = new XMLHttpRequest();
xhr.open("POST", "http://myserver/data/add/", true);
xhr.setRequestHeader("Content-type", "application/json");
xhr.setRequestHeader("Content-length", data.length);
xhr.setRequestHeader("Connection", "close");
xhr.setRequestHeader('Remote-User', 'myuser');
xhr.onreadystatechange = function() {
  if (xhr.readyState == 4) {
      console.log('xhr response: '+ xhr.responseText);
  }
}
xhr.send(data);