节点在JSON请求中将Array转换为String

时间:2015-01-17 09:07:50

标签: jquery ajax node.js express

我正在向Node.js API发送JSON请求。

$.ajax({
    url : url,
    contentType: "application/json",
    type: "POST",
    data: {
        "userId":1002,
        "type":"from",
        "actors": [1001],
        "actual_amount":5.00,
        "last_modified":1421480903128
    },
    success : function(data) {
        //Success
    },
    error : function(err) {
        //Error
    }
});

在Node.API中收到请求Obj:

actors: "[1001]"
actual_amount: "5.00"
last_modified: "1421480903128"
type: "from"
userId: "1002"



var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');


app.use(bodyParser.json({ type: 'application/*+json' }));
app.use(bodyParser.urlencoded({ extended: true }));

请告诉我如何解决此问题。我是Node.js的新手

1 个答案:

答案 0 :(得分:1)

您需要JSON.stringify您的data对象,以便它以JSON的形式发送,而不仅仅是一串key = value参数:

$.ajax({
    url : url,
    contentType: "application/json",
    type: "POST",
    data: JSON.stringify({
        "userId":1002,
        "type":"from",
        "actors": [1001],
        "actual_amount":5.00,
        "last_modified":1421480903128
    }),
    success : function(data) {
        //Success
    },
    error : function(err) {
        //Error
    }
});