express接收post请求参数

nodejs代码:

//安装依赖:npm install body-parser express --save-dev

var express = require("express");
var bodyParser = require('body-parser');//解析,用req.body获取post参数
var app = express();
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({extended: false}));

app.post("/",function(req,res){
    console.log(JSON.stringify(req.body));
    res.send({hello:'world'});
})

请求接口:

//jq
    $.ajax({
        url:"http://192.168.1.200",
        method:"post",
        data:{
            a:1,
            b:2
        }
    }).done(function(d){
        console.log(d)
    })
//原生js
    var xml=new XMLHttpRequest();
        xml.open("POST","http://192.168.1.200",true)
        xml.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xml.send("a=1&b=2");
        xml.onreadystatechange=function(){
            if (xml.readyState==4&&xml.status==200) {
                console.log(xml.responseText);
            }
        }

你可能感兴趣的:(express接收post请求参数)