Maifee Ul Asad
Maifee Ul Asad

Reputation: 4579

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.

Upvotes: 3

Views: 1357

Answers (1)

Maifee Ul Asad
Maifee Ul Asad

Reputation: 4579

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.

Upvotes: 2

Related Questions