2016-10-11 89 views
0

我有這個代碼在我的反應的應用程序,它沒有發佈json的身體! 已經嘗試了每個解決方案,即使這裏的一個https://github.com/matthew-andrews/isomorphic-fetch/issues/34也不能工作,我可以用jquery做這個帖子,但是我需要使用fetch,任何想法?isomorophic-fetch不發佈json主體

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 */ 
     }); 
+0

什麼是服務器的響應? – Shota

+0

在僞造的API上使用您的代碼時,它可以正常工作。我無法重現該問題。你確定你的API工作正常嗎? http://jsbin.com/foyanir/edit?js,console –

+0

服務器無法獲得json正文,使用jquery時效果很好,所以服務器代碼沒問題, –

回答

0

嘗試了一切可能之後(如sugguested編輯代碼,檢查瀏覽器的最新和工作,檢查服務器代碼,圖書館工作將與GET方法,但POST仍無法正常工作),我換回到我們老,愛JQuery!