ASP.NET webmethod with multiple parameters from jQuery

I am attempting to post data from the page back to my server method.  Unfortunately I am getting a series of different errors which seem to be centered around my .ajax:data serialization.  I have tried passing the data using paramname:value and also by passing a single query string, and as a single string to be split at the server.

Desperate to figure out how this works.
client side jQuery Script:
//trying this to see if I can pass a single string
        var params = code + ":" + collector + ":" + branch + ":" + manager + ":" + client;
//param names on server side are the same as params client side
params=params.replace(/ /,"~");
        $.ajax({
            type: "POST",
            url: "ChangeCodeAssignment2.aspx/saveUpdate",
            data: {"test"},
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            dataFilter: function(data) {
                var msg;

                if (typeof (JSON) !== 'undefined' &&
                            typeof (JSON.parse) === 'function')
                    msg = JSON.parse(data);
                else
                    msg = eval('(' + data + ')');

                if (msg.hasOwnProperty('d'))
                    return msg.d;
                else
                    return msg;
            },
            error: function(msg) {
                alert(msg.responseText);
            },
            success: function(msg) {
                alert(msg);
            }
        });


//******************************************************************
//******************************************************************
//******************************************************************

//server side script
    [WebMethod]
    public static string saveUpdate(string code)
    {
        code = code.Replace('~', ' ');
        List<string> items = new List<string>(code.Split(':'));
        var a = items[0];
        var b = items[1];
        var c = items[2];
        var d = items[3];
        var e = items[4];
        
        return "Successful";
    }

Open in new window

LVL 10
GeoffSuttonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
this:
data: {"test"},

is the equivalent of having a <form> field named "test" and it's value is empty. So essentially you are sending ONE empty field to the server. If you are trying to pass params then use:
data: {"test":params},
0
GeoffSuttonAuthor Commented:
Same response.  I used:

url: "ChangeCodeAssignment2.aspx/saveUpdate",
data: {"code":params},
contentType: "application/json; charset=utf-8",
And got the error response:

{"Message":"Invalid JSON primitive: code.","StackTrace":"   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n   at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n   at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}
Which unfortunately tells me nothing.  As you can see, I am completely new to jQuery.
Thanks
 
0
hieloCommented:
but the params you are sending:
var params = code + ":" + collector + ":" + branch + ":" + manager + ":" + client;

is NOT a json encoded string. It sounds like your server expects a json-encoded string. What you have there is a colon delimited string. So on the server you should just treat is as a "regular" string and split it at the colons.

Otherwise you will need an actual json encoded string:

var params = '{"code":"'+code+'", "collector":"'+collector+'", "branch":"'+branch+'", "manager":"'+manager+'", "client":"'+client+'"}';

...
 data: {"test":params},

then on the server, parms should be a json-encoded string.

NOTE:
 dataType: "json",

That does NOT take the current parameters and transforms them to a json string before they are sent TO the server. By setting the dataType to json, you (the developer) are "promissing" the browser that the result you will be returning from the server will be a json encoded string. So be sure you returned a json string from the server.

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

GeoffSuttonAuthor Commented:
From what I understood the webmethod returned JSON encoded already.  But thanks for the information - I will modify the query and give that a try tomorrow.  
Thanks for the heads up on this.
Geoff
0
hieloCommented:
>>From what I understood the webmethod returned JSON encoded already
OK. Just wanted to make sure you were not misunderstanding the jquery  dataType: "json" option
0
GeoffSuttonAuthor Commented:
That worked perfectly.  Thanks so much for the info on JSON notation.  I appreciate it.
Geoff
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.