我想在node.js完成向我的数据库插入记录后用我的ajax打印出来的东西。比如“console.log('it working')”但遗憾的是,在nodejs完成插入记录后,ajax无法打印出来。
AJAX
$(document).ready(function (e) {
$("#new-user-form").on('submit',(function(e) {
e.preventDefault();
var data = {};
data.title = $('#title').val();
data.fname = $('#fname').val();
data.mname = $('#mname').val();
data.lname = $('#lname').val();
data.mdname = $('#mdname').val();
$('#loading').show();
//toastr.success('Page Loaded!');
$.ajax({
url: "/new-user",
type: "POST",
data: data,
dataType: 'application/json',
cache: false,
success: function(data) {
console.log('it working');
$('#loading').hide();
}
});
}));
});
新-user.js的
var express = require('express'),
router = express.Router(),
db = require('./../db');
router.post('/', function (req,res,next) {
var title = req.body.title;
var first_name = req.body.fname;
var middle_name = req.body.mname;
var last_name = req.body.lname;
var maiden_name = req.body.mdname;
var sql = db.insert({first_name:first_name,last_name:last_name}).into('users').then(function(data){
res.send(data);
});
if(sql){
console.log('records inserted');
}else{
console.log('insertion failed');
}
});