【发布时间】:2017-04-11 00:29:23
【问题描述】:
我对这一切(包括 Javascript 回调和 ES6)有点陌生。我正在使用 NodeJS + Express + MongoDB。
我正在调用一个 Ajax 函数来更新一个项目,但成功的 Ajax 调用从未完成。
这是我的 Ajax 调用(从 React 调用)
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?在 jquery 1.6 上添加了 Promise
-
@freedomn-m
fail()未被调用。 -
@guyfawkes jquery 3.1.1
标签: javascript jquery node.js ajax