我正在尝试从运行在Node.JS上的服务器端代码中获取JSON中的查询数据
这是我的客户端AJAX请求:
$(function() {
var values = $(this).serialize();
$.ajax({
url: '/querySearch',
type: 'post',
data: values,
success: function(msg){
console.log(msg);
},
error: function(){
alert('failure');
}
});
});
我的serserside代码:
app.post('/querySearch', function(req, res) {
var queryNumber = Number(req.body.queryNumber);
if (queryNumber == 1){
executeQuery1(res, sendQueryResults);
}
else if (queryNumber == 4){
executeQuery4(res, sendQueryResults);
}
else if (queryNumber == 6){
executeQuery6(res, sendQueryResults);
}
else if (queryNumber == 7){
executeQuery7(res, sendQueryResults);
}
});
function executeQuery1(res, callback) {
var query = "" +
'query string';
service.oneshotSearch(query, {}, function(err, results) {
if (err) {
console.log(err);
alert("An error occurred with the search");
return;
}
callback(res, results);
});
});
}
function sendQueryResults(res, results) {
res.json(JSON.stringify(results));
res.end();
}
但是,目前,我的网页甚至无法完成加载。我做错了什么?