使用fetch进行post提交数据,数据没有提交上去。 - CNode技术社区

使用fetch进行post提交数据,数据没有提交上去。
发布于 9 年前 作者 Zuckjet 4553 次浏览 来自 问答

应用场景: 本人使用mockjs模拟后台数据,前端页面通过fetch发起post请求提交数据,该请求返回的http状态码为201,但是返回的数据为空,也就是说数据没有提交到后台去。 后来本人换用jquery提交数据,成功提交了。 我的问题是: 1.是因为mockjs不支持fetch的post的请求,还是说我使用fetch语法存在错误,请各位指正。 发送请求代码如下:

n.png

2 回复

我记得fetch post的时候要显式指定hearder的content-type,你可以试下

 fetch(API.articles, {
 method: 'POST',
 headers: {
 'Content-Type': 'application/json',
 },
 body: JSON.stringify({
 title: this.title,
 content: this.content,
 slug: this.slug
 })
 })
 .then(action((response) => {
 if (response.status >= 400) {
 throw new Error(response.statusText)
 }
 return 'OK'
 }))

你的是urlencode自己对应改下

回到顶部

AltStyle によって変換されたページ (->オリジナル) /