我无法让我的服务器使用node.js 和express 从客户端调用函数。我不需要传递数据,我只需要提醒服务器它应该调用一个函数。这就是我所拥有的(在遵循大量教程之后):
客户:
$.ajax({
type: 'POST',
url: 'http://localhost:3001/admin',
sucess: function() {
console.log('sucess');
}
服务器:
var express = require('express');
var app = express();
var server = require('http').Server(app);
app.set('port', process.env.PORT || 3001);
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hjs');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
app.post('/admin', function(req, res) {
console.log("admin refresh");
res.send(200);
});
错误:
POST http://localhost:3001/admin 404 (Not Found)
最佳答案
您的中间件顺序错误。按申报顺序快速处理它们。将您的 app.post('/admin...
行移至 Not Found 中间件上方。
关于ajax - Node.js 和 Express;后404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26516022/