node.js - express接受不到客户端传来的json
PHP中文网
PHP中文网 2017-04-17 15:06:49
0
2
548

客户端代码:

var sendData = JSON.stringify({
            name: document.forms["token"].name.value,
            password:document.forms["token"].password.value
        });
$.ajax({
        url:'http://localhost:8088/log/',
        type:'POST',
        contentType:'application/json',
        data: sendData,
        dataType:"json"
    })

node:

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.all("/log",function(req,res){
    var name = req.body.name;
    var password = req.body.password;
    console.log(name,password)
    ...
 }

发现name,password都是undefined,客户端传的json正常:

{"name":"fafa","password":"afa"}

请问这是怎么回事呢?谢谢

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(2)
洪涛

In this scenario, it seems that json.stringify is not needed

阿神
var obj = JSON.parse(req.body)
console.log(obj.name, obj.password)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template