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??

2
  • You have one problem to begin with: your JSON is not valid. Strings in JSON are in double quotes. Commented Feb 11, 2014 at 7:13
  • thanks for pointing it out. i have fixed it but now getting another error. i have posted the new problem here. Commented Feb 11, 2014 at 7:43

4 Answers 4

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
    }
Sign up to request clarification or add additional context in comments.

2 Comments

can you please explain what is have to do in server apart from creating class. lets say "insertNewBookingInfo" is function name in the server
for too long character in my comment, i must post it as answer, see answer
0

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

Comments

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

1 Comment

changing string to dynamic solved my problem. thanks for the help
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.