鍍金池/ 問答/C++  HTML/ node express 獲取jsonArray取不到值

node express 獲取jsonArray取不到值

Node獲取ajax post上來的json數(shù)據(jù),其數(shù)據(jù)格式如下:
var dataJSON = {

"test": "sssssssssssssssss",
"action": [{
        "action": "hotV1.action",
        "data": "111"
    },
    {
        "action": "hotV2.action",
        "data": "222"
    },
    {
        "action": "hotV3.action",
        "data": "333"
    }
]

}

Node中,使用require('body-parser')后,
app.post('/test', function(req, res) {

console.log(req.body.test);
console.log(req.body);

});
輸出見下圖
圖片描述

直接req.body.test取出沒問題,但是要取出action就取不到值,求各位大解惑。。。。

app.js

var express = require('express');
var path = require('path');
var app = express();
var bodyParser = require('body-parser');
/**
 * 引入模塊
 */
var requestFun = require('./requestFun');
  
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.use(express.static(path.join(__dirname, 'public')));
//設(shè)置跨域訪問
app.all('*', function(req, res, next) {
    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");
    next();
 });


app.post('/test', function(req, res) {
    console.log(req.body.test);
    console.log(req.body);
    //requestFun.setJson(req.body);
});

app.get('/',function(req,res){
    res.render('index')
});

app.listen(process.env.PORT || 3000); 

index.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
    </head>
    <body>
    </body>
    <script>
    $(function(){
        var dataJSON = {
            "test": "sssssssssssssssss",
            "action": [{
                    "action": "hotV1.action",
                    "data": "111"
                },
                {
                    "action": "hotV2.action",
                    "data": "222"
                },
                {
                    "action": "hotV3.action",
                    "data": "333"
                }
            ]
        }

        // var dataJSON = {
        //     "test":"ssssssssssssssssss",
        //     "hotV1":{
        //         "action": "hotV1.action",
        //         "data": "111"
        //     },"hotV2":{
        //         "action": "hotV2.action",
        //         "data": "222"
        //     },"hotV3":{
        //         "action": "hotV3.action",
        //         "data": "333"
        //     }
        // }

        $.ajax({
            type:'post',
            url:'http://localhost:3000/test',
            data:dataJSON,
            dataType: "json",
            success:function(data){
                console.log(data);
            },
            error:function(){
                console.log('error');
            }
        })
    });
    </script>
</html>
回答
編輯回答
尐懶貓

console出來的object明顯key不對,key為action[0][action]而不是action,看起來像你post的content-typeapplication/x-www-form-urlencoded而不是application/json

2017年11月14日 16:02