var http = require('http');
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io').listen(server);
var path = require('path');
var mysql = require('mysql');
var bodyParser = require('body-parser');
var jsonParser = bodyParser.json({type: 'application/json'});
var cookieParser = require('cookie-parser');
var cors = require('cors');
app.use('*', cors());
app.set('views', path.join(__dirname, 'views'));
app.use(express.static(path.join(__dirname, 'public')));
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json({type: 'application/json'}));
app.use(cookieParser());
这是我使用jquery API的客户端代码
$.ajax({
url: "/checkIfAdmin",
type: 'GET',
data: JSON.stringify({'password': 'password', 'username': 'username'}),
crossDomain: true,
async: true,
cache: false,
contentType: "application/json; charset=utf-8",
processData: false,
success: function (data) {
// doing something
},
error: function(data){
// throw error here!
}
});
身体解析器一直在返回一个空的身体,现在正试图解决这个问题一段时间,在下面的应用程序路线上
app.get('/checkIfAdmin', jsonParser, function(req, res){
console.log(req.body);
// Am getting an empty object ie {}
});
得到{}(空对象) 请问我该如何解决这个问题
答案 0 :(得分:0)
它获得了API。因此,您将获得req.query中的数据。