问题
I got following format when i send post request from postman.
{
ID:"66",
Blod:"test",
Allergic:"no",
Chronic:"no"
}
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.
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);
});
}
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());
回答1:
you don't need to use 'JSON.stringify' , like this:
submithandler=(e)=>{
e.preventDefault();
axios.post('http://localhost:8000/api/addsickers',
{
ID:'123456789',
Blod:'22334445',
Allergic:'6677788',
Chronic:'3445566'
},
)
.then(response=>{
alert(response);
})
.catch(err=>{
alert("catch"+err);
});
}
回答2:
In your code you don't need this code:
// parse the raw data
app.use(bodyparser.raw());
// parse text
app.use(bodyparser.text());
As the documentation states:
bodyparser.raw() Returns middleware that parses all bodies as a Buffer and only looks at requests where the Content-Type header matches the type option.
And
bodyParser.text() Returns middleware that parses all bodies as a string and only looks at requests where the Content-Type header matches the type option. This parser supports automatic inflation of gzip and deflate encodings.
You have to change that line from:
bodyparser.urlencoded({ extended: false });
to:
app.use(bodyparser.urlencoded({ extended: false });
回答3:
Please check the postman request configuration settings as follows.
If the problem you are facing now is that how to process incoming react request, the following code snippet will helps you.
router.post('/', (req, res) => {
let request = JSON.parse(JSON.stringify(req.body));
console.log(request.ID);
});
回答4:
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)
}
//}
}
and in api I did :
let request = JSON.parse(req.body)
console.log(request);
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.
thank you all for your help.
来源:https://stackoverflow.com/questions/61705905/json-post-method-format