簡體   English   中英

獲取json數據時出錯,ajax

[英]Error getting json data, ajax

我試圖從服務器獲取數據而不使用ajax刷新頁面,所以數據的問題就像文本不像json數據

我的代碼:

$.ajax({
    type: "GET",
    url: "http://localhost:8080/search?key=" + QUERY + "",
    success: function (reslt) {
        console.log(reslt);
        console.log(reslt.length);
    }
});

和服務器上的數據:

即時通訊使用nodejs和表達框架的代碼:

router.get('/search', function (req, res) {
    tab = ['08:00', '09:00', '10:00', '11:00'];
    res.end(JSON.stringify(tab));
});

為什么當我做console.log(reslt[3]); 它給我8 ,應該給我10:00

采用

dataType: 'json'

如果您的響應是JSON,請始終將datatype設置為json 像這樣做

$.ajax({
    type: "GET",
    dataType: 'json',
    url: "http://localhost:8080/search?key=" + QUERY + "",
    success: function (reslt) {
        console.log(reslt);
        console.log(reslt.length);
    }
});

您必須在ajax請求中使用dataTypecontentType屬性

$.ajax({
    type: "GET",
    dataType: 'json',
    contentType: "application/json",
    url: "http://localhost:8080/search?key=" + QUERY + "",
    success: function (reslt) {
        console.log(reslt);
        console.log(reslt.length);
    }
});

暫無
暫無

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

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