gpt4 book ai didi

jquery - ServiceStack 反序列化在 jQuery 请求上失败,但在 C# 客户端上失败

转载 作者:行者123 更新时间:2023-12-01 01:16:50 26 4
gpt4 key购买 nike

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

这是他通过 jquery ajax 调用传递的 json

{"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);

然后就可以了。任何想法为什么会有差异?

最佳答案

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

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

关于jquery - ServiceStack 反序列化在 jQuery 请求上失败,但在 C# 客户端上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12932795/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com