ServiceStack反序列化对jQuery请求失败,但对C#客户端失败

时间:2012-10-17 10:56:08

标签: jquery json servicestack

我的前端实施者遇到了一个相当复杂的嵌套DTO的奇怪问题。

这是json,他正在通过jquery ajax调用

{"Id":"507e7e5aa6305825c012c606","Name":"test fried chicken","Description":"why you no work","Servings":4,"Author":"unit test","Steps":[{"Instructions":"put chicken in egg","Ingredients":[{"PreparationId":"507e7e34a6305825c012c601","MeasureId":"kilogram","Quantity":2,"MeasureType":"weight"},{"PreparationId":"507e7e36a6305825c012c605","MeasureId":"gram","Quantity":100,"MeasureType":"weight"}]},{"Instructions":"put in flour and cook it up","Ingredients":[{"PreparationId":"507e7e35a6305825c012c603","MeasureId":"gram","Quantity":100,"MeasureType":"weight"}]}]}

这是对uri / recipes的PUT,然后在请求端转换为DTO的默认实例(实际上没有信息被反序列化)。

如果我从请求中获取完全相同的Json并运行C#代码

var d = new ServiceStack.Text.JsonSerializer<RecipeDTO>();
var re = d.DeserializeFromString(theJson);
var client = new JsonServiceClient(uri);
client.Put<RecipeDTOResponse>("/recipes", re); 

然后它的工作原理。任何想法为什么会有区别?

1 个答案:

答案 0 :(得分:3)

在记录来自网页和C#客户端的IHttpRequest之后,我注意到jquery调用的ContentType是 application / x-www-form-urlencoded ,而c#是 application / JSON

在jquery调用中更改它解决了这个问题。