gpt4 book ai didi

javascript - NodeJS 函数在 ajax 调用上不返回响应

转载 作者:行者123 更新时间:2023-12-03 06:44:05 25 4
gpt4 key购买 nike

我有一个名为 db.js 的数据库文件,我将其导出到我的路线并运行查询。该代码有一个回调函数来等待ajax响应。当我运行时,它永远不会结束,ajax 调用将永远运行。

有人知道为什么以及如何解决这个问题吗?

谢谢

db.js 文件

var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit : 100,//To update with the max of allowed connection after that the new connections are made in the queue
host : 'localhost',
user : 'root',
password : '',
database : 'nodejs',
debug : false
});
function handle_database(query, return_data, callback) {
pool.getConnection(function(err,connection){
if (err) {
connection.release();
callback(null, false);
}
connection.query(query,function(err,rows){
connection.release();
if(!err) {
if(return_data){
callback(null, JSON.stringify(rows));
}
}else{
callback(null, false);
}
});
connection.on('error', function() {
callback(null, false);
});
});
}
module.exports = handle_database;

app.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);

app.post('/assign', function(req, res){
var query = "INSERT INTO assigned_stops (jsons) VALUES ('"+JSON.stringify(req.body)+"')";
handle_database(query, false, function(err, response){
var obj = {};
console.log(response);
if(response !== false){
obj['status'] = "OK";
obj['message'] = "Data successfully assigned.";
obj['title'] = "Success";
obj['refresh'] = true;
res.send(JSON.stringify(obj));
}else if(response === false){
obj['status'] = "KO";
obj['message'] = "An error occured! Try later please.";
obj['title'] = "Error";
obj['refresh'] = false;
res.send(JSON.stringify(obj));
}
});
});

// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});


module.exports = app;

我的查看代码:

$.ajax({
cache: false,
type: 'POST',
data: JSON.stringify(assignmentsOBJ),
contentType: 'application/json',
url: '/assign',
beforeSend: function() {
dialogRef.getModalBody().html('Request processing, please wait...');
},
success: function(data) {
dialogRef.close();
var response = JSON.parse(data);
if(response.status === "OK") {
fncShowDialog(response.message, BootstrapDialog.TYPE_SUCCESS, response.title, response.refresh);
}
else if(response.status === "KO") {
fncShowDialog(response.message, BootstrapDialog.TYPE_ERROR, response.title, response.refresh);
}
},
error: function() {
dialogRef.close();
fncShowDialog("An error occured! Try later please.", BootstrapDialog.TYPE_ERROR, "Error", false);
},
complete: function() {
dialogRef.close();
}
});

最佳答案

此行 app.use('/', paths); 覆盖您的下一条路线 app.post('/assign', function(req, res){}) 当您请求在路由器 (./routes/index) 中“/分配”快速应用程序搜索处理程序时。删除路由器或在“/assign”之后调用

关于javascript - NodeJS 函数在 ajax 调用上不返回响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37827113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com