Search code examples
javascriptcurlpostgoogle-chrome-extensionxmlhttprequest

post data from chrome extension


I have a rest method to receive data from user, here is the CURL command:

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

Now I'm trying to POST data, using java-script:

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);

But this receives nothing.

I'm using this java-script in a Chrome-Extension.


Solution

  • Here is my current code :

    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);
    

    It works, problem was in my rest api.