用nodejs——express模板搭建了1个demo.(配置了跨域) app.js 中配置如下: app.use(bodyParser.json({type:'application/*+json'})); app.use(bodyParser.text()); app.use(bodyParser.urlencoded({ extended: false })); routers/index.js中的路由如下: router.post('/post',function(req,res,next){ console.log("body=="+JSON.stringify(req.body)); console.log("name="+req.body.name); console.log('gender='+req.body.gender); res.send('ok'); } 在另外一个文件夹中新建了一个html,引用的jquery: $.ajax( { type:"post", dataType:"json", contentType: 'application/json', url:"http://192.168.X.XXX:8000/post",(nodejs服务器的地址) data:{"name":"111","gender":"女"}, ... } 结果是:能正常访问服务器返回的数据,但是服务器端拿不到req.body的值,显示为body=={}
我把$.ajax中的的contentType 删除后(contentType默认为application/x-www-form-urlencoded),就可以拿到req.body的值,显示body=={"name":"111","gender":"女"}
这是为什么?不能自定义contentType的值?