Ajax请求失败
问题描述:
邮政请求待处理。 我想记录传入请求的JSON正文。使用邮差当 请求工作,但是AJAX请求不工作Ajax请求失败
节点代码:
var express = require('express');
var morgan = require('morgan');
var bodyParser = require('body-parser');
var hostname = 'localhost';
var port = 3000;
var app = express();
app.use(morgan('dev'));
var qRouter = express.Router();
qRouter.use(bodyParser.json());
qRouter.route('/')
.all(function(req,res,next) {
res.header('Access-Control-Allow-Origin', "*")
res.writeHead(200, { 'Content-Type': 'application/json' });
next();
})
.post(function(req,res){
console.log(req.body);
res.end("lalal");
})
app.use('/play',qRouter);
app.listen(port, hostname, function(){
console.log(`Server running at http://${hostname}:${port}/`);
});
Ajax请求
var d = {
"rating": 5,
"comment": "Sends anyone to heaven, I wish I could get my mother-in-law to eat it!",
"author": "Paul McVites"
}
$.ajax({
url: 'http://localhost:3000/play',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(d)
});
答
我认为你不处理你的Ajax调用你的回应jQuery的。您正在发送数据,但我看不到您收到服务器响应。 POSTMAN自动为你处理。
+0
这似乎不是问题所在。 –
它为什么不起作用?肯定有一个错误或一些指标。 –
请求始终保持挂起状态,idk为什么 –
以及节点内的哪些函数按什么顺序被调用? –