【发布时间】:2015-08-17 16:32:21
【问题描述】:
我正在尝试从服务器获取数据而不使用 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'添加到$.ajax()调用中。 -
将
dataType:'json'添加到您的ajax call -
在服务器端使用 JSON.stringify 并在成功处理程序中使用 JSON.parse 来访问数据的索引。
标签: javascript jquery ajax json node.js