我对所有这些(包括Javascript回调和ES6)有点新。我正在使用NodeJS + Express + MongoDB。
我打电话给一个Ajax函数来更新一个项目,并且成功的Ajax调用从未完成。
这里是我的Ajax调用(从阵营调用)
editBug : function(bug){
console.log('about to edit bug with these values',bug);
$.ajax({
url:'/api/bugs',
method: 'PUT',
data:bug
})
.done((jqxhr) => {
console.log('succcess while editing the bug');
this.setState({successVisible : true});
})
.fail((jqxhr) => {
console.log('error : ' + jqxhr);
})
},
这里是我的API函数:
app.put('/api/bugs',function(req,res){
//console.log('req',req);
console.log('query string : ',req.query);
console.log('query params : ',req.params);
console.log('query body: ',req.body);
let id = new ObjectID(req.body._id);
req.body._id = new ObjectID(req.body._id);
db.collection('bugs').replaceOne(
{_id:id},
req.body,
function(err,result){
assert.equal(err,null);
console.log('Successfull replace!');
res.status(200);
}
);
});
的Successfull replace!
日志在服务器端正确显示。 正面显示about to edit bug with these values
正面。但succcess while editing the bug
日志不显示在前端,似乎.done
调用永远不会执行。
您不提及是否调用了fail()。 –
你使用的是什么版本的jquery? Promise被添加到jquery 1.6 –
@ freedomn -m'失败()'不被调用。 – nicolasdaudin