在我的代码中,ajax post方法的成功部分无法正常工作,我不确定为什么。这个ajax调用是通过jquery的fullcalendar函数的一部分。
ajax请求将数据发送到发布路由,并将其用于数据库cal(mysql)。数据传递和相应的sql查询工作正常,但是ajax调用的成功部分无法正常工作(如console.log输出不足所证明)
下面是ajax调用的代码:
$.ajax({
url:"/insertEventsIntoDirectorInput",
type:"POST",
data:{title:title, start:start, end:end, urlPath: window.location.href.match(/\/([^/]*)$/)[1]},
success:function()
{
console.log('insert success function started');
calendar.fullCalendar('refetchEvents');
alert("Added Successfully");
}
})
以下是发布路线的代码:
router.post('/insertEventsIntoDirectorInput', function (req, res) {
if (req.body.title) {
var urlVariable = req.body.urlPath;
console.log(urlVariable);
console.log(typeof urlVariable);
var title = req.body.title;
var start = req.body.start;
var end = req.body.end;
dbjsMethods.geteIDFromDirectorInputForm(urlVariable, function(eID) {
console.log(eID);
console.log(typeof eID);
dbjsMethods.insertEventsInDirectorInput(eID[0].eID,title,start,end);
})
console.log(res);
}
});
以下是sql查询的代码(通过node.js):
geteIDFromDirectorInputForm: function (url, callback) {
console.log('geteID function started');
console.log(url);
conn.query(`SELECT eID FROM directoravailabilitiesinputform where url = ?`, [url],
function (err, results, fields) {
if (err) throw err;
console.log(results);
return callback(results);
});
},
insertEventsInDirectorInput: function (eID,title,start,end) {
console.log('insert function started');
console.log(typeof eID);
conn.query('insert into directorinputevents(eID,title,start,end) values ("' +eID+ '","' +title+ '","' +start+ '","' + end + '")',
function (err, results, fields) {
if (err) throw err;
else console.log('Updated ' + results.affectedRows + ' row(s).');
});
},
运行该函数时,我希望在第一行代码中会出现console.log所指示的“插入成功函数已启动”的输出,但这不存在。
答案 0 :(得分:1)
您在服务器上的.post()
处理程序不会发送响应。客户端仍在等待服务器的回音。您需要始终从服务器发送回响应。
router.post('/insertEventsIntoDirectorInput', function (req, res) {
if (req.body.title) {
var urlVariable = req.body.urlPath;
console.log(urlVariable);
console.log(typeof urlVariable);
var title = req.body.title;
var start = req.body.start;
var end = req.body.end;
dbjsMethods.geteIDFromDirectorInputForm(urlVariable, function(eID) {
console.log(eID);
console.log(typeof eID);
dbjsMethods.insertEventsInDirectorInput(eID[0].eID,title,start,end);
res.sendStatus(200);
})
console.log(res);
} else {
res.sendStatus(500);
}
});
在这里我插入了res.sendStatus(xxx)
,因为它看起来好像客户端没有期望从请求中返回任何数据,因此这只是将状态代码发送回而没有数据。
仅供参考,您还需要修复geteIDFromDirectorInputForm()
方法以正确返回错误。
geteIDFromDirectorInputForm: function (url, callback) {
console.log('geteID function started');
console.log(url);
conn.query(`SELECT eID FROM directoravailabilitiesinputform where url = ?`, [url],
function (err, results, fields) {
if (err) {
callback(err);
} else {
console.log(results);
callback(null, results);
}
});
},
然后,更改调用代码以查看该错误:
router.post('/insertEventsIntoDirectorInput', function(req, res) {
if (req.body.title) {
var urlVariable = req.body.urlPath;
console.log(urlVariable);
console.log(typeof urlVariable);
var title = req.body.title;
var start = req.body.start;
var end = req.body.end;
dbjsMethods.geteIDFromDirectorInputForm(urlVariable, function(err, eID) {
if (err) {
console.log(err);
res.sendStatus(500);
} else {
console.log(eID);
console.log(typeof eID);
dbjsMethods.insertEventsInDirectorInput(eID[0].eID, title, start, end);
res.sendStatus(200);
}
});
} else {
res.sendStatus(500);
}
});