问题描述
我正在尝试通过AAJAX调用将数据发送/更新到mongoDB数据库,但是命令没有到达theere。我尝试在代码之间使用Alert进行调试,但命令未到达该位置。意味着AJAX调用不会被执行。
以下是我的AJAX POST请求代码:
var text = "Done";
var data = {
selectedValue: text
}
$ajax({
method: 'POST',url: '/update-sources',dataType: 'text/json',data: data,success: function(data){
console.log(data);
alert("Working!!")
}
});
下面是/ update-sources路由代码:
router.post('/update-sources',function(req,res,next) {
console.log("/Update-Sources")
User.findOneAndUpdate({email: req.user.email},{$set:{status:data.selectedValue}},{new: true},(err,doc) => {
if (err) {
console.log("Something wrong when updating data!");
}
else
{
res.render('taskswriter');
console.log(doc);
return "Great Working!";
}
});
});
我在做什么错?
解决方法
如果您共享浏览器的控制台输出,那就太好了,但是尝试执行附加的客户端代码段时,出现以下错误:
VM219:7 Uncaught ReferenceError: $ajax is not defined
at <anonymous>:7:1
您在这里有一个错字-当您从 jQuery名称空间(https://api.jquery.com/jQuery.ajax/)访问函数时,它应该是$.ajax