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

前端 未结 5 549
闹比i
闹比i 2021-02-09 22:29

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

Actually i can get and use the

相关标签:
5条回答
  • 2021-02-09 23:04

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

    data: JSON.stringify(myObj)
    

    Step2, Server-side: You have to translate the serialized object into something "eatable" by your c# code. Here you can use deserialize() method of Microsoft's JavaScriptSerializer class (but it might have some issues in .net 3.5 if you don't have SP installed), or otherwise JSON.net library http://james.newtonking.com/pages/json-net.aspx

    server-side method signature should be:

    fSaveToDB(Object myObj)
    

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

    {myObj: your object...}
    
    0 讨论(0)
  • 2021-02-09 23:06

    Is your asmx page expecting a soap envelope? If so you will have a tough time connecting directly to it with an xmlhttp request do to the additional markup (it's certainly do-able, but is a pain). You may want to look at some examples of creating restful services as they will be easier to communicate with via javascript. Take a look at http://www.west-wind.com/weblog/posts/324917.aspx.

    0 讨论(0)
  • 2021-02-09 23:13

    A detailed answer for a similar question states that combining the JQuery & Json2.stringfy() can be used for sending complex type to Server-Side methods.

    And on the Server Side, you will only need to put the required Type in the Method Signature (ex. foo(MyType obj) { ... } )

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

    0 讨论(0)
  • 2021-02-09 23:31

    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 is:

    [WebMethod()]
    public Student fSaveToDB(Student _obj)
    {
        return bla bla bla;
    }
    
    0 讨论(0)
  • 2021-02-09 23:31

    Let's simplify this: You have a server side object instance name and a client side object instance name.

    In your jQuery ajax - use this form

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

    On the server side use

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

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

    0 讨论(0)
提交回复
热议问题