[英]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.