JSON POST请求仅发送id

时间:2018-05-21 17:09:42

标签: reactjs api post

Hello Everyone我找不到任何解决方案,所以这是我的问题 我尝试编写POST请求并尝试将数据作为JSON发布,它可以工作 但是我只用新的ID在JSON文件中获取新的Object,没有其​​他任何东西被发送。

这是准确的ReaactJS应用程序

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

这是我这样做的方法

2 个答案:

答案 0 :(得分:0)

您应该查看新的抓取API。

Fetch API提供了一个用于获取资源的接口(包括整个网络)。使用XMLHttpRequest的人似乎很熟悉,但新API提供了更强大,更灵活的功能集。

请检查Fetch: POST json data

答案 1 :(得分:0)

问题在于 .rest 文件的格式 例如

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

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

这对我有用。您需要在 content-type 后留一个空行,然后指定对象。 以下对我不起作用,仅发送 ID。

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

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