[英]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
請求中使用dataType
和contentType
屬性
$.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.