0

Hello Everyone I couldn't find any solution so here is my question I tried to write POST Request and tried to POST data as JSON, it works but I only get new Object in JSON file with new ID, nothing else is being sent.

This is ReaactJS app to be exact

              var url = 'http://localhost:3000/applications'; //Configurable endpoint
   var request = new XMLHttpRequest();
   var isSent = false;
   
   var data = {
       name : this.state.name,
       age : this.state.age,
       prefix : this.state.prefix,
       email : this.state.email
   }
   
   var xhr = new XMLHttpRequest();
   xhr.open('POST', url, true);
   xhr.onload = function () {
       isSent = true;
       console.log(this.responseText);
   };
   xhr.send(data);

This is my way to do so

Przemo
  • 35
  • 5

2 Answers2

0

You should check out new fetch API.

The Fetch API provides an interface for fetching resources (including across the network). It will seem familiar to anyone who has used XMLHttpRequest, but the new API provides a more powerful and flexible feature set.

Please check Fetch: POST json data

Johnson Samuel
  • 2,046
  • 2
  • 18
  • 29
0

The problem night be with the formatting of the .rest file For example

POST  http://localhost:3001/api/persons
Content-Type: application/json

{
"name" : "fbgjdbh",
"number" : 89475947
}

this worked for me. You need to leave an empty line after the content-type, then specify the object. The following was not working for me and was only sending the id.

POST  http://localhost:3001/api/persons

Content-Type: application/json
{
"name" : "fbgjdbh",
"number" : 89475947
}