admin管理员组文章数量:1279011
fetch('/auth/signup', {
method: 'POST',
body: this.state.user,
headers: {
'Content-Type': 'application/json'
},
})
.then(function(response) {
return response.json();
})
.then(data => {
console.log(data);
})
this.state.user is basically an object like {user: {name:"someone"}
and my server side code simply do this
router.post('/signup', (req, res, next) => {
console.log(req.body) // empty object
}
I think something is wrong with fetch
, didn't see any data been pass in the network tab.
fetch('/auth/signup', {
method: 'POST',
body: this.state.user,
headers: {
'Content-Type': 'application/json'
},
})
.then(function(response) {
return response.json();
})
.then(data => {
console.log(data);
})
this.state.user is basically an object like {user: {name:"someone"}
and my server side code simply do this
router.post('/signup', (req, res, next) => {
console.log(req.body) // empty object
}
I think something is wrong with fetch
, didn't see any data been pass in the network tab.
- Do you see a certain message in your [console] window? – Marylyn Lajato Commented Apr 22, 2017 at 3:31
- @eeya 400 error, bad request – Jenny Mok Commented Apr 22, 2017 at 3:32
- On your [fetch] method, what happens when you replace the url to 'signup' instead of 'auth/signup'? – Marylyn Lajato Commented Apr 22, 2017 at 3:37
- @eeya now I manage to pass data but server return req.body as {} hmmm... – Jenny Mok Commented Apr 22, 2017 at 3:40
1 Answer
Reset to default 8Pass this.state.user
to JSON.stringify()
before setting at body
or use FormData
to POST
key, value pairs. body
does not expect javascript
object.
body: JSON.stringify(this.state.user)
let fd = new FormData();
let {user} = this.state.user;
for (let prop in user) {
fd.append(prop, JSON.stringify(user[prop]));
}
body: fd
本文标签: javascriptcan39t do POST to apierror 400 using fetchStack Overflow
版权声明:本文标题:javascript - can't do POST to api, error 400 using fetch - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741300663a2371073.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论