简体   繁体   English

FormData:在没有JSON.stringify的情况下分配对象

[英]FormData: assign object without JSON.stringify

I want send to server data in this format: 我希望以这种格式发送到服务器数据:

{"post"=>{"body"=>"hello", "title"=>"321", "username"=>"1"}

But If I use FormData in this way: 但是,如果我以这种方式使用FormData:

var data = new FormData();
undefined
var a = {username: "1", title: "1", body: "1"}
undefined
data.append( 'post', a );
undefined
data.get("post")
"[object Object]"

I will get this: 我会得到这个:

{"post"=>"[object Object]"}

How I can to send data in right format? 如何以正确的格式发送数据?

You are appending "a", but "a" is an object, specifically, a JSON object, which is why you see the {"post"=>"[object Object]"} . 您附加“a”,但“a”是一个对象,特别是JSON对象,这就是您看到{"post"=>"[object Object]"} Use JSON.parse(a) first to build your string. 首先使用JSON.parse(a)来构建字符串。 See JSON parse 请参阅JSON解析

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM