“检查”页面时,可以在“网络”选项卡中看到从服务器发送的响应。但是,控制台不会将响应记录为“成功”。 Node.js作为后端。以下是代码:
function getFilteredData(){
$(function( $ ){
console.log("getting table data");
var filters ={};
//inserted some data in filters
$.ajax({
type : 'POST',
url : "query/get-filtered-data",
dataType : 'application/json',
async: "true",
data:JSON.stringify(filters),
contentType: 'application/json; charset=utf-8',
success : function(data){
console.log(data);
// doesn't log anything.
//server response is visible in networks section of 'inspect' in browser.
},
failure: function(data){
alert('got an error');
}
});
});
}
我将“过滤器”作为数据发送到服务器端。在那里,我使用这些过滤器使用猫鼬查询mongodb集合。
router.post('/get-filtered-data', bodyParser.json(), function (req, res) {
console.log(req.body);
var filters = req.body;
// authorModel is my mongoose model.
authorModel.find({})
.then(function (doc) {
console.log(doc); //prints the correct response
res.send(doc);
});
});
代码是否有问题或我缺少什么?提前谢谢。
编辑: 添加了我进行ajax调用的功能。该功能位于HTML文件中。
更新
感谢@Quentin,现在可以使用了。将ajax调用中的数据类型更改为dataType:'json'
。