2016-06-23 149 views
7

使用react和webpack ..爲什麼下面的代碼會導致錯誤:Uncaught (in promise) SyntaxError: Unexpected end of input(…)?謝謝Fetch no-cors意外的輸入結束

fetch(feedURL, {"mode": "no-cors"}) 
    .then(response => response.json()) 
    .then(function(data){ 

     this.setState({ 
      data: data 
     }) 

    }.bind(this)); 
+0

是響應有效的JSON? – frankies

+1

由於您沒有檢查您正在獲取的響應類型,也沒有設置任何接受標頭,因此當您獲得的響應不是JSON類型時,您可能沒有故障保護。可能是問題的可能原因。 –

+0

測試JSON原始數據是有效的,但託管在CDN上的文件的URL無效。 '編碼無效,期待UTF-8,UTF-16或UTF-32。「關於我如何解決的任何想法? – user14439

回答

0

爲了更好地理解您的錯誤,請在抓取請求中添加一個catch案例。另外,如果你使用箭頭函數,你不需要綁定(this);如果你使用箭頭函數,你不需要綁定(this);如果你使用箭頭函數,你不需要綁定(this)。

fetch(feedURL, {"mode": "no-cors"}) 
.then(response => response.json()) 
.then(data => { 
    this.setState({ 
     data: data 
    }); 
}) 
.catch(resp => { 
    console.error(resp); 
});