簡體   English   中英

意外令牌 < 在 JSON 中 position 0

[英]Unexpected token < in JSON at position 0

如果我執行 curl,服務器會返回一個帖子對象數組,如下所示:

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"
            }
        }
    }
]

我嘗試使用 fetch 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);  
        });
}

但我收到此錯誤:

意外令牌 < 在 JSON 中 position 0

SyntaxError: Unexpected token < in JSON at position 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);  
        });

請檢查您的服務器是否僅以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獲取所有數據。 有關更多詳細信息,請單擊此鏈接

錯誤消息的措詞與您運行JSON.parse('<...')時從Google Chrome獲得的內容相對應。 我知道您說服務器正在設置Content-Type:application / json,但是我被認為是響應主體實際上是HTML。

“ SyntaxError:JSON中位置0處的意外令牌<”

在console.error(this.props.url,status,err.toString())下划線。 錯誤實際上是在jQuery中引發的,並作為變量err傳遞給您。 帶下划線的原因僅僅是因為這就是您要記錄的地方。

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

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

因此,您可以將其與start JSON array一起使用;

{

"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"
            }
        }
    }
]

}

所以問題是這樣的:由於我是從LAN上的無業游民機器中測試它的,而在我的主機文件中,我添加了無業游民機器的IP作為URL(test.com),因此該設備無法獲取該URL。 在將無所事事的機器更改為端口轉發並提供了機器的原始IP地址之后,我能夠獲取json對象。 謝謝大家的幫助。

您可以嘗試添加dataType屬性,就像dataType: 'html'dataType: 'text'

對我來說這是因為端口已經在使用中,您可以嘗試通過在 cmd(管理員模式)下運行以下命令來終止端口

taskkill /F /IM node.exe

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM