我正在向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的新手
答案 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
}
});