Node.js 从Dart接收节点JS服务器上的POST请求时,会给出带有空值和
这是我在dart侧的代码。我创建了一个对象,并尝试使用Node.js 从Dart接收节点JS服务器上的POST请求时,会给出带有空值和,node.js,dart,networking,Node.js,Dart,Networking,这是我在dart侧的代码。我创建了一个对象,并尝试使用jsonecode方法将该对象作为请求主体发送 class RZPOrder { final int orderId; final int amount; RZPOrder({this.orderId, this.amount}); Map toJson() => {'orderId': orderId, 'amount': amount}; } String url = 'http://876b98959b91.
jsonecode
方法将该对象作为请求主体发送
class RZPOrder {
final int orderId;
final int amount;
RZPOrder({this.orderId, this.amount});
Map toJson() => {'orderId': orderId, 'amount': amount};
}
String url = 'http://876b98959b91.ngrok.io';
print(jsonEncode(order));
final http.Response res = await http.post(
url,
headers: <String, String>{
'Content-Type': 'application/x-www-form-urlencoded',
},
body: jsonEncode(order),
);
这里的print语句是{“orderId”:555,“amount”:3500}:''}
使用邮递员时,我得到的输出与dart相同
请帮助我在节点端获取正确的json字符串。谢谢。在dart端将
内容类型更改为'application/json'
,并使用app.use(bodyParser.json())节点端的代码>解决了这个问题
但是,我仍然无法理解为什么在使用x-www-form-urlencoded
时在节点端更改json字符串
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded());
app.route('/')
.post(function(req, res){
console.log(req.body);
var options = {
amount: req.body.amount,
receipt: req.body.orderId
};
});