json post方法格式 [英] json post method format
问题描述
当我向邮递员发送邮寄请求时,我得到了以下格式.
I got following format when i send post request from postman.
{
ID:"66",
Blod:"test",
Allergic:"no",
Chronic:"no"
}
但是当我使用 react app post 方法发送 post 请求时,我得到了这种格式.
But i got this format when i send post request using react app post method.
[Object: null prototype]
{
'{
"ID":"123456789",
"Blod":"22334445",
"Allergic":"6677788",
"Chronic":"3445566"}': ''
}
请帮助我如何获得相同格式的邮递员以正确插入数据.
please help me how can I got the same format of postman to insert data correctly.
这是我从 react app uisng axios 模块的方法:
this is my method from react app uisng axios module :
submithandler=(e)=>{
e.preventDefault();
axios.post('http://localhost:8000/api/addsickers',
JSON.stringify({
ID:'123456789',
Blod:'22334445',
Allergic:'6677788',
Chronic:'3445566'
}),
)
.then(response=>{
alert(response);
})
.catch(err=>{
alert("catch"+err);
});
}
我在 api 上使用解析
I use parsing on api
app.use(bodyparser.json());
// parse application/x-www-form-urlencoded
bodyparser.urlencoded({ extended: false });
// parse the raw data
app.use(bodyparser.raw());
// parse text
app.use(bodyparser.text());
推荐答案
我已经通过使用 fetch 方法解决了这个问题,如下所示:
I have solved this by using fetch method like the following :
senddata(event){
event.preventDefault();
//if(!this.formvalidation())
//{
try{
fetch('https://localhost:8000/api/addsickers/',{
method:'post',
mode:'no-cors',
headers:{
'Accept':'application/json',
'Content-type': 'application/json'
},
body:JSON.stringify({
ID:this.state.code,
Blod:this.state.blod,
Allergic:this.state.allergic+" - "+this.state.allergicdescription,
Chronic:this.state.chronic+" - "+this.state.chronic_description
})
});
alert("data goes")
}catch(e){
alert(e)
}
//}
}
在 api 中我做了:
and in api I did :
let request = JSON.parse(req.body)
console.log(request);
并且我得到了相同形式的邮递员工具,它让我可以正确地为 insert 调用属性,例如:request.ID 返回正确的值.
and I got the same forma of postman tool that let me to call attributes correctly for insert , for example : request.ID return the correct value.
感谢大家的帮助.
这篇关于json post方法格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!