0

lets say i a json object and a json array. i need it to pass those as ajaxdata to asp.net.

Below is myCode Code

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);
      }

  }

Server Code

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

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

For Bigger Picture click here

now im getting this error

what is the problem. am i missing something??

MD TAHMID HOSSAIN
  • 1,581
  • 7
  • 29
  • 54

4 Answers4

1

why dont you stringify your data in client, and deserializing at server?

in client:

data: JSON.stringify(dataAjax)

and in server:

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

and MyClass type is :

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
    }
Amir Sherafatian
  • 2,083
  • 2
  • 20
  • 32
0

in server, your data you passed is string (JSON format) and you need to convert this, to specific class to access your property you want typesafe, if you have a object in javascript like this

{name: "mark", age: 35}

when you stringify your object that be changed to

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

and when you passed this to server, you need to convert this string to a class, to access properties you want, and for this purpose, you need to a server side class, which its properties conform to name and type of your javascript object properties, something like this

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
    }

form more help follow: Deserialize-JSON-with-Csharp and json-string-to-c-sharp-object

Community
  • 1
  • 1
Amir Sherafatian
  • 2,083
  • 2
  • 20
  • 32
0

with the help of am1r_5h. i found that changing incoming datatype to dynamic solved my problem

public static string insertNewBookingInfo(dynamic jsonstaticobj1232, dynamic jsonarray646) 
        {

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

Solved My Problem.

MD TAHMID HOSSAIN
  • 1,581
  • 7
  • 29
  • 54
0

and also i have posted here a answer to using WebMethod i think be useful to you, WebMethod to async ajax request

Amir Sherafatian
  • 2,083
  • 2
  • 20
  • 32