1

我需要将 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":""}]}"

调试,我看到它被分成

密钥(Request.Form.GetKey(0)):

{"Files":[{"Title":"Test","OriginalFileName":"","FileName":"http://company.domain.com/auth.aspx?enrollmentkey

值(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 数据是否需要在 = 处转义,还是需要对整个内容进行编码,或者我是否遗漏了什么?

我应该注意我正在使用淘汰赛的 ko.toJSON(js) 来创建 JSON,尽管我不确定这是否相关。

我还注意到 chrome 开发工具似乎也可以识别 Key-Val 拆分:

在此处输入图像描述

4

1 回答 1

2

如果要将 JSON 数据发送到服务器,则 Content-Type 标头需要设置为application/json. 如果设置为,application/x-www-form-urlencoded则服务器将尝试将 JSON 解释为 URL 中的键值对。这就是为什么您的 JSON 字符串在=.

于 2013-09-04T15:16:17.997 回答