Javascript 如何在deleteajax[NODE.JS]中使用成功回调
我正在使用以下代码删除数据库中的集合: 客户:Javascript 如何在deleteajax[NODE.JS]中使用成功回调,javascript,jquery,ajax,node.js,Javascript,Jquery,Ajax,Node.js,我正在使用以下代码删除数据库中的集合: 客户: $('.destroy').click(function() { if(confirm("Are u sure?")) { $.ajax({ type: 'DELETE', url: '/destroy/' + dataId, success: function(response) { console.log('Succ
$('.destroy').click(function() {
if(confirm("Are u sure?")) {
$.ajax({
type: 'DELETE',
url: '/destroy/' + dataId,
success: function(response) {
console.log('Success');
}
});
} else {
alert('Cancelled');
}
});
服务器:
app.get('/destroy/:id', function(req, res) {
var id = req.param("id");
MyModel.remove({
_id: id
}, function(err){
if (err) {
console.log(err)
}
else {
console.log('Collection removed!');
}
});
});
正在工作,如果我单击销毁按钮并重新加载页面,集合将不在那里,但是带有:[console.log('success');]
的成功回调函数没有运行
我需要从服务器发送一个回调到客户端ir,以使成功函数运行
如何制作console.log(“Success”);跑
谢谢。ajax调用可能只是超时,因为它永远不会从服务器得到响应 从服务器发送响应
app.get('/destroy/:id', function(req, res) {
var id = req.param("id");
MyModel.remove({
_id: id
}, function(err){
if (err) {
res.end('error');
}
else {
res.end('success');
}
});
});
然后抓住它
$.ajax({
type : 'DELETE',
url : '/destroy/' + dataId,
success : function(response) {
if ( response === 'error' ) {
alert('crap!');
} else if (response === 'success' ) {
alert('worked fine!');
}
}
});
这是一个简化的示例,您可以返回任何您喜欢的内容,发送状态码或任何内容。ajax调用可能只是超时,因为它永远不会从服务器得到响应 从服务器发送响应
app.get('/destroy/:id', function(req, res) {
var id = req.param("id");
MyModel.remove({
_id: id
}, function(err){
if (err) {
res.end('error');
}
else {
res.end('success');
}
});
});
然后抓住它
$.ajax({
type : 'DELETE',
url : '/destroy/' + dataId,
success : function(response) {
if ( response === 'error' ) {
alert('crap!');
} else if (response === 'success' ) {
alert('worked fine!');
}
}
});
这是一个简化的示例,您可以返回任何您喜欢的内容,发送状态码或任何内容。ajax调用可能只是超时,因为它永远不会从服务器得到响应 从服务器发送响应
app.get('/destroy/:id', function(req, res) {
var id = req.param("id");
MyModel.remove({
_id: id
}, function(err){
if (err) {
res.end('error');
}
else {
res.end('success');
}
});
});
然后抓住它
$.ajax({
type : 'DELETE',
url : '/destroy/' + dataId,
success : function(response) {
if ( response === 'error' ) {
alert('crap!');
} else if (response === 'success' ) {
alert('worked fine!');
}
}
});
这是一个简化的示例,您可以返回任何您喜欢的内容,发送状态码或任何内容。ajax调用可能只是超时,因为它永远不会从服务器得到响应 从服务器发送响应
app.get('/destroy/:id', function(req, res) {
var id = req.param("id");
MyModel.remove({
_id: id
}, function(err){
if (err) {
res.end('error');
}
else {
res.end('success');
}
});
});
然后抓住它
$.ajax({
type : 'DELETE',
url : '/destroy/' + dataId,
success : function(response) {
if ( response === 'error' ) {
alert('crap!');
} else if (response === 'success' ) {
alert('worked fine!');
}
}
});
这是一个简化的示例,您可以返回任何您喜欢的内容,发送状态码或其他内容