我需要将JSON数据发布到包含URL的MVC控制器。 JSON数据看起来像是在查询字符串(=)
处拆分JSON数据如下所示:
"{"Files":[{"Title":"test","OriginalFileName":"",
"FileName":"http://company.domain.com/auth.aspx?enrollmentkey=APK54cd1546a8454d4ca79ded89a78f8698",
"Categories":[{"CategoryId":76,"SubCategoryId":182,"CatId":"CatId0"}],
"TypeId":"84",
"Tags":["Select Tag(s)..."],
"TagIds":[],
"Roles":[],
"MemberOnly":false,
"ContentTypeId":7,
"Id":0,
"IsPublished":true,
"PublishDate":""}]}"
调试,我看到它被拆分为
KEY(Request.Form.GetKey(0)):
{"Files":[{"Title":"Test","OriginalFileName":"","FileName":"http://company.domain.com/auth.aspx?enrollmentkey
VALUE(Request.Form.GetValue(0)):
APK54cd1546a8454d4ca79ded89a78f8698","Categories":[{"CategoryId":110,"SubCategoryId":111,"CatId":"CatId0"}],"TypeId":"69","Tags":["Select Tag(s)..."],"TagIds":[],"Roles":[],"MemberOnly":false,"ContentTypeId":7,"Id":0,"IsPublished":true,"PublishDate":""}]}
JSON数据是否需要在=处进行转义,或者需要对整个事物进行编码,或者我是否遗漏了某些内容?
我应该注意到我正在使用knockout的ko.toJSON(js)来创建JSON,尽管我不确定它是否相关。
我还注意到chrome dev工具似乎也认识到了Key-Val分裂:
答案 0 :(得分:2)
如果要将JSON数据发送到服务器,则需要将Content-Type标头设置为application/json
。如果设置为application/x-www-form-urlencoded
,则服务器将尝试将JSON解释为URL中的键值对。这就是为什么你的JSON字符串在=
处被破解为两个。