【问题标题】:isomorophic-fetch don't post json bodyisomorphic-fetch 不要发布 json body
【发布时间】: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


【解决方案1】:

尝试了所有可能的方法后(根据建议编辑代码,检查浏览器是否最新并正常工作,检查服务器代码,库将使用 GET 方法工作,但 POST 仍然无法工作)我切换回旧的并喜欢 JQuery !

【讨论】:

    猜你喜欢
    • 2017-02-04
    • 2016-10-22
    • 2020-08-12
    • 2021-05-28
    • 2017-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    相关资源
    最近更新 更多