简体   繁体   English

从javascript发送json到asp.net

[英]sending json from javascript to asp.net

I have a web app where the backend is made in ASP.net. 我有一个Web应用程序,后端在ASP.net中进行。 The ajax call is a normal get request Ajax调用是正常的获取请求

$.ajax({
    url: 'myurl/updatejson',
    contentType: "application/json; charset=utf-8",
    data: data,
    success: function (data) {
        // do something
    },
    error: function (xhr, status, err){
        console.error(xhr, status, err);
    }
});          

Where data is a simple key, value object correctly formatted. 如果数据是简单键,则值对象的格式正确。 The C# backend is C#后端是

public HttpStatusCodeResult UpdateJson(Dictionary<string, object> json){ //do something }

from this i was expecting that json in c# would be equal to key, value but instead i get key, [value] for some reason. 从这个我希望C#中的json等于key, value但是出于某种原因key, [value]我却得到了key, [value] If i declare the Dictionary as Dictionary<string, string> instead of Dictionary<string, object> everything works fine and i get the expected result, so it mean that for some reason is converting the string to object wrapping it in an array. 如果我将Dictionary声明为Dictionary<string, string>而不是Dictionary<string, object>一切正常,并且我得到了预期的结果,所以这意味着出于某种原因将字符串转换为将其包装在数组中的对象。

How can i use the dictionary<string, object> (because the value could be a string, int or boolean) without having the value wrapped in an array? 我如何在没有将值包装在数组中的情况下使用dictionary<string, object> (因为该值可以是字符串,int或boolean)?

Have you tried to use dynamic type resolution ? 您是否尝试过使用动态类型解析?

 public HttpResponseMessage UpdateJson([FromBody]dynamic value){
    ...
    Console.WriteLine(value["field1"]);
 }

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

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