Skip to content Skip to sidebar Skip to footer

Sending Data Over Chrome Extension To Rest

I am trying to send my selection to a server using post and server need REST, Tested my data with Advanced Rest Client and all works well, But when I try from my chrome extension I

Solution 1:

I think you misunderstand the purpose of chrome.runtime.sendMessage. See the first two paragraphs of the Message Passing documentation. sendMessage is used for "communication between extensions and their content scripts" it is not designed for sending an HTTP request.

A content script must also "set up a runtime.onMessage event listener to handle the message" only then will you receive a valid response from the sendMessage request

The result from their example is undefined

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

If you are attempting to perform a HTTP request from within your chrome extension, you should use XHR

var data = $.toJSON({'content': 'the paste content'});

var xhr = newXMLHttpRequest();
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);

Post a Comment for "Sending Data Over Chrome Extension To Rest"