Passing javascript object to webservice via Jquery ajax

Posted by kralco626 on Stack Overflow See other posts from Stack Overflow or by kralco626
Published on 2010-12-31T16:58:52Z Indexed on 2010/12/31 17:54 UTC
Read the original article Hit count: 240

Filed under:
|
|
|
|

I have a webservice that returns an object

[WebMethod]
    public List<User> ContractorApprovals()

I also have a webservice that accepcts an object

[WebMethod]
    public bool SaveContractor(Object u)

When I make my webservice calls via Jquery:

function ServiceCall(method, parameters, onSucess, onFailure) {
    var parms = "{" + (($.isArray(parameters)) ? parameters.join(',') : parameters) + "}"; // to json
    $.ajax({
        type: "POST",
        url: "services/"+method,
        data: parms,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            if (typeof onSucess == 'function' || typeof onSucess == 'object')
                onSucess(msg.d);
        },
            error: function(msg, err) {
            $("#dialog-error").dialog('open');}
});

I can call the first one just fine. My onSucess function gets passed a javascript object exactly structured like my User object on the service.

However, I am now having trouble getting the object back to the server.

I'm accepting Object as a parameter on the server side so I can't inagine there is an issue there. So I'm thinking something is wrong with the parms on the client side but i'm not sure what...

I am doing something to the effect

ServiceCall("AuthorizationManagerWorkManagement.asmx/ContractorApprovals",
      "",
      function(data,args){$("#div").data('user',data[0])},
      null)

then

ServiceCall("AuthorizationManagerWorkManagement.asmx/SaveContractor",
      $("#div").data('user'), //These also do not work: "{'u': ' + $("#div").data("user") + '}", NOR JSON.stringify({u: userObject})
      function(data,args){(alert(data)},
      null)

I know the first service call works, I can get the data. The second one is causing the "onFailure" method to execute rather then "OnSuccess".

Any ideas?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery