【问题标题】:How to pass a jsonObject and a json array as ajaxdata to Asp.net如何将 jsonObject 和 json 数组作为 ajaxdata 传递给 Asp.net
【发布时间】:2014-02-11 07:10:08
【问题描述】:

假设我是一个 json 对象和一个 json 数组。我需要它将这些作为 ajaxdata 传递给 asp.net。

下面是我的代码

function insertNewBookingInfo(jsonstaticobj1232, jsonarray646) {
      try {
          debugger;
         //jsonstaticobj1232 is a json object
         //jsonarray646 is a json array
          var dataAjax = { "jsonstaticobj1232": jsonstaticobj1232, "jsonarray646": jsonarray646 };

          console.log(JSON.stringify(dataAjax));
          jQuery.ajax({
              url: 'WalkReserve.aspx/insertNewBookingInfo',
              type: "POST",
              data: JSON.stringify(dataAjax),
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function (data) {
                  debugger;
                  console.log(data.d);
              },
              error: function (result) {
                  debugger;
                  console.log('Failed' + result.responseText);
              }

          });
      } catch (e) {
          debugger;
          console.log(e.message);
          alert(e.message);
      }

  }

服务器代码

 [WebMethod]
            public static string insertNewBookingInfo(string jsonstaticobj1232, string jsonarray646) //, string jsonarray646
            {

       // BAL.insertNewBookingInfo(jsonstaticobj1232, jsonstaticobj1232); //, jsonarray646
        return "ABC";
    }

For Bigger Picture click here

现在我得到这个错误

有什么问题。我错过了什么吗??

【问题讨论】:

  • 您首先遇到了一个问题:您的 JSON 无效。 JSON 中的字符串用双引号括起来。
  • 感谢您指出。我已经修复了它,但现在又出现了另一个错误。我已经在这里发布了新问题。

标签: jquery json webforms


【解决方案1】:

你为什么不在客户端stringify你的数据,并在server反序列化?

在客户端:

data: JSON.stringify(dataAjax)

在服务器中:

System.Web.Script.Serialization.JavaScriptSerializer o = new System.Web.Script.Serialization.JavaScriptSerializer();
o.Deserialize<MyClass>(stringified_dataAjax);
//OR
o.ConvertToType<MyClass>(stringified_dataAjax);

MyClass 类型是:

public class MyClass
    {
        // you need to change 'int' type to type which exactly they are
        public int jsonstaticobj1232 { get; set; } // int => your 'reservation' object type probably
        public int jsonarray646 { get; set; } // int => List<T> : T is your corresponding object
    }

【讨论】:

  • 你能解释一下除了创建类之外在服务器中必须做的事情吗?假设“insertNewBookingInfo”是服务器中的函数名称
  • 我评论中的字符太长,我必须将其发布为答案,请参阅答案
【解决方案2】:

我还在这里发布了使用WebMethod 的答案,我认为这对你有用, WebMethod to async ajax request

【讨论】:

    【解决方案3】:

    在服务器中,您传递的数据是 string (JSON format),如果您在 javascript 中有这样的对象,您需要将其转换为特定类以访问您想要类型安全的属性

    {name: "mark", age: 35}
    

    当您将要更改为的对象字符串化时

    "{"name":"mark","age":35}"
    

    当你将它传递给服务器时,你需要将此字符串转换为一个类,以访问你想要的属性,为此,你需要一个服务器端类,它的属性符合你的名称和类型javascript 对象属性,类似这样

    class anyClasName // it doesn't matter
        {
            public string name { get; set; } // the name of property must be exactly same as your javascript object property
            public byte age { get; set; }    // and the type of property should be something which property of javascript object can be convert to
        }
    

    更多帮助请关注:Deserialize-JSON-with-Csharpjson-string-to-c-sharp-object

    【讨论】:

    • 将字符串更改为动态解决了我的问题。感谢您的帮助
    【解决方案4】:

    在 am1r_5h 的帮助下。我发现将传入的数据类型更改为动态解决了我的问题

    public static string insertNewBookingInfo(dynamic jsonstaticobj1232, dynamic jsonarray646) 
            {
    
                // BAL.insertNewBookingInfo(jsonstaticobj1232, jsonstaticobj1232); //, jsonarray646
                return "ABC2";
            }
    

    解决了我的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-24
      • 2021-01-17
      • 2015-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-08
      相关资源
      最近更新 更多