2016-08-08 27 views
0

如果我捲曲,服務器返回的帖子對象的數組,像這樣:意外標記<在JSON在位置0

curl http://localhost/api/posts/ 
[ 
    { 
     "id": 7, 
     "target": { 
      "body": "This is the body", 
      "title": "Airbnb raising a reported $850M at a $30B valuation", 
      "user": { 
       "first_name": "ben", 
       "last_name": "jamin" 
      } 
     } 
    }, 
    { 
     "id": 11, 
     "target": { 
      "body": "This is the body", 
      "title": "Browsing the APISSS", 
      "user": { 
       "first_name": "user", 
       "last_name": "two" 
      } 
     } 
    } 
] 

我嘗試這樣使用提取API獲得:

fromServer() { 
    console.log('fromServer') 
    var headers = new Headers(); 
    headers.append('Accept', 'application/json'); 
    let a = fetch('http://test.com/api/posts/', headers) 
     .then(function(response) { 
       if (response.status !== 200) { 
        console.log('There was a problem. Status Code: ' + response.status); 
        return; 
       } 

       response.json().then(function(data) { 
        console.log(data); 
       }); 
      } 
     ) 
     .catch(function(err) { 
      console.log('Fetch Error :-S', err); 
     }); 
} 

但我得到這個錯誤:

Unexpected token < in JSON at position 0

SyntaxError: Unexpected token < in JSON at position 0

我該如何解決這個問題?你可以幫我嗎。謝謝。

+0

有機會,您收到一個HTML文檔 - 嘗試'response.text()',而不是'response.json()'看你真的在收到 –

+0

,完全取決於你 –

+0

我認爲它應該是 - > 「.then(function(response)=> {...});」,you missing「=> 」。只是一個猜測。 – Rajesh

回答

0

所以,問題是這樣的:因爲我是從流浪漢機LAN,並在我的主機測試它的文件我加無業遊民機的IP作爲URL(test.com),該設備無法以獲取該網址。在我將流浪機更改爲端口並給出機器的原始IP地址後,我可以獲取json對象。感謝大家的幫助。

0

很明顯,您在將響應解析爲json對象時有一些語法錯誤。

刪除服務器json文件中的所有評論(如果有的話)。如果不是: 我相信響應正文不是json。

您正在從服務器接收HTML(或XML),但是代碼能夠解析爲JSON。 檢查Chrome開發工具中的「網絡」選項卡,查看服務器響應的內容。

或使用此代碼的調試:(爲「Jaromanda X」表示)

.then(function(response) { 
       console.log(response); 
       console.log(response.status); 
       console.log(response.json()); 
       console.log(response.text()); 
       }) 
     .catch(function(err) { 
      console.log('Fetch Error :-S', err); 
     }); 
0

錯誤消息的措詞與您從谷歌瀏覽器得到什麼,當你運行JSON.parse(」 < .. 。')。我知道你說服務器正在設置Content-Type:application/json,但我被引導認爲響應體實際上是HTML。

「語法錯誤:在JSON意外標記<在0位置」

與線路console.error(this.props.url,狀態,err.toString())加下劃線。 err實際上是在jQuery中引發的,並作爲變量err傳遞給你。這條線被加下劃線的原因僅僅是因爲那是你記錄它的地方。

我建議你添加到你的日誌記錄。查看實際的xhr(XMLHttpRequest)屬性以瞭解更多關於響應的信息。嘗試添加console.warn(xhr.responseText),你很可能會看到正在接收的HTML。

請根據上述說明更正您的代碼。

0

請檢查您的服務器是否僅以JSON格式提供響應。 對於關聯數據,您應該使用「{」不是「[」來啓動您的JSON對象。 所以你的數據應該採用下面的格式。

{ "data":[ { "id": 7, "target": { "body": "This is the body", "title": "Airbnb raising a reported $850M at a $30B valuation", "user": { "first_name": "ben", "last_name": "jamin" } } }, { "id": 11, "target": { "body": "This is the body", "title": "Browsing the APISSS", "user": { "first_name": "user", "last_name": "two" } } } ] } 而且您可以從response.data獲取所有數據。 欲瞭解更多詳情,請按照this link

0

等等,你可以用啓動JSON數組來使用;

{ 

"array": [ 

    { 
     "id": 7, 
     "target": { 
      "body": "This is the body", 
      "title": "Airbnb raising a reported $850M at a $30B valuation", 
      "user": { 
       "first_name": "ben", 
       "last_name": "jamin" 
      } 
     } 
    }, 
    { 
     "id": 11, 
     "target": { 
      "body": "This is the body", 
      "title": "Browsing the APISSS", 
      "user": { 
       "first_name": "user", 
       "last_name": "two" 
      } 
     } 
    } 
] 

} 
0

你可以嘗試添加dataType財產,就像dataType: 'html'dataType: 'text'

相關問題