【发布时间】:2016-10-11 09:46:03
【问题描述】:
我的反应应用程序中有这段代码,但它没有发布 json 正文! 已经尝试了每一种解决方案,即使是这里的https://github.com/matthew-andrews/isomorphic-fetch/issues/34 也不起作用,我可以用 jquery 来发帖,但我需要使用 fetch,有什么想法吗?
fetch('/main/newuser',{
method:'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body:JSON.stringify({username:'test',password:'test'})
}).then((response)=>{
if(response.status >= 200 && response.status < 300){
return response;
}else{
var error = new Error(response.statusText);
error.response = response;
throw error;
}
}).then((response)=>{
return response.json();
}).then((data)=>{
/* Process data */
}).catch((error)=>{
/* Catch Error */
});
【问题讨论】:
-
服务器的响应是什么?
-
在假 API 上使用您的代码时,它可以工作。我无法重现该问题。你确定你的 API 工作正常吗? jsbin.com/foyanir/edit?js,console
-
服务端获取不到json body,使用jquery时运行良好,所以服务端代码没问题,
-
您是否尝试过使用不同的 fetch polyfill?例如来自 Github 的github.com/github/fetch
-
isomorophic-fetch 是建立在 fetch polyfill 之上的,你认为可以解决问题吗?反正现在就试试吧
标签: json post reactjs fetch isomorphic-fetch-api