Sending a JSON object to an ASP.NET web service using jQuery ajax function

I want to create an object on the client side of an aspx page. And I want to add functions to these javascript classes to make life easier.

In fact, I can get and use objects (obtained from classes on the server side) that are returned from services. When I wanted to send objects from the client using jQuery ajax methods, I could not do this :)

These are my javascript classes:

function ClassAndMark(_mark, _lesson){ this.Lesson = _lesson; this.Mark = _mark; } function Student(_name, _surname, _classAndMark){ this.Name = _name; this.SurName = _surname; this.ClassAndMark = _classAndMark; } 

And this is the method for the Student class to call the web service:

 JSClass.prototype.fSaveToDB(){ $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "/WS/SaveObject.asmx/fSaveToDB"), data: ????????????, // This might be: JSON.stringify(this) ? // Web service method has a parameter, name is _obj // if i don't send data with parameter, i'm getting this error: // Invalid web service call, missing value for parameter: '_obj' // // Should i send it like that: // data: "{_obj:" + JSON.stringify(this) + "}" // // I tried to wrap this with parameter like that: // data: JSON.stringify("{_obj:" + this + "}") // // But i got this error: // Cannot convert object of type 'System.String' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]' dataType: "json" }); } 

To create a javascript object and call its method to send it to a web service:

Actually, I don’t know what the definition of server-side classes and methods should be, but I think:

 class ClassAndMark{ public string Lesson ; public string Mark ; } class Student{ public string Name ; public string SurName ; public ClassAndMark classAndMark ; } 

The web service is lower, but again I could not get what should be instead ????

 [WebMethod()] public Student fSaveToDB(???? _obj) { // How can i convert input parameter/parameters // of method in the server side object? // SQL operations // srting QInsert = "INSERT INTO tableName (......) VALUES (.....)"; // ... // .. // . return new Student{ Name = ???, // deserialize _obj and pass its Name value SurName = ???, // deserialize _obj and pass its SurName value classAndMark = ???, // deserialize _obj and pass its classAndMark value }; } 
+6
json jquery c # web-services
source share
5 answers

Step 1, client side: you have to serialize your client objects in JSON, personally I use the stringify () method of the JSON2 library: http://www.json.org/js.html

 data: JSON.stringify(myObj) 

Step 2, on the server side: you need to translate the serialized object into something "edible" by your C # code. Here you can use the deserialize () method of the Microsoft JavaScriptSerializer class (but it can have some problems in .net 3.5 if you don't have SP installed), or else the JSON.net library http://james.newtonking.com/pages/json -net.aspx

the server side signature should be:

 fSaveToDB(Object myObj) 

where "myObj" is the name of your client-side object container:

 {myObj: your object...} 
+5
source share

Is your asmx page expecting an envelope with soap? If so, it will be difficult for you to connect directly to it using the xmlhttp request for additional markup (this certainly does, but it is pain). You can look at some examples of creating calm services, since it will be easier to communicate with them using javascript. Take a look at http://www.west-wind.com/weblog/posts/324917.aspx .

0
source share

Javascript Side:

 JSClass.prototype.fSaveToDB(){ $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "/WS/SaveObject.asmx/fSaveToDB"), data: data: $.toJSON({ _obj: this }), dataType: "json" }); } 

Web service:

 [WebMethod()] public Student fSaveToDB(Student _obj) { return bla bla bla; } 
0
source share

A detailed answer to a similar question indicates that combining JQuery and Json2.stringfy () can be used to send complex types to Server-Side methods.

And on the server side you only need to put the required type in the method signature (for example, foo (MyType obj) {...})

How to send a JSON object to asp.net web service and process the data there?

0
source share

Let it simplify: You have the server instance object name and the client side object instance name.

In jQuery ajax - use this form

 data: '{"myServerSideObjectInstanceName":' + JSON.stringify(myClientSideObjectInstanceName) + '}', 

Server side use

 public void MyWebServiceMethod(myObject myServerSideObjectInstanceName) { ... your code here ...} 

As long as myObject has an identical signature as your javascript Object, this will work. You can get your values ​​(on the server) using something like myServerSideObjectInstanceName.StudentName (or something else).

0
source share

All Articles