前端發送json數組

舉個例子:前端

前端:node

$.ajax({
            type: "POST",
            url:"http://localhost:8080/json",
            dataType:'json',
            data:"da="+JSON.stringify([{"name":"zhan"},{"age":"25"}])
//            data:{"da":JSON.stringify([{"name":"zhan"},{"age":"25"}])}
        })

後端(nodejs):ajax

引入body-parser中間件express

const express=require('express');
var bodyParser = require('body-parser');
const server=express();
server.use(bodyParser.urlencoded({extended: false}));
server.use(bodyParser.json());
server.listen(8080);
server.post('/json',function(req,res){
     //cros資源共享
    //res.header("Access-Control-Allow-Origin", "*");//全部站點均可以訪問該資源
    //res.header("Access-Control-Allow-Headers", "X-Requested-With");
    //res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    //res.header("X-Powered-By",' 3.2.1')
    //res.header("Content-Type", "application/json;charset=utf-8");
    res.send("ok").end();
    console.log(req.body);
    console.log(JSON.parse(req.body.da)[0].name)
})
相關文章
相關標籤/搜索