获取json数据时出错,ajax
我试图从服务器获取数据,而不使用ajax
刷新页面,所以问题的数据来像文本不像json
数据
我的代码:
$.ajax({ type: "GET", url: "http://localhost:8080/search?key=" + QUERY + "", success: function (reslt) { console.log(reslt); console.log(reslt.length); } });
和服务器上的数据:
即时通讯使用nodejs和expression框架的代码:
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); } });