Click here to Skip to main content
15,888,968 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi.

I´ve seen many tutorials that talks about jquery JSON calls to a webservice.
Basically many of them suggest that you make a webservice call like this:

$.ajax({
        type: "POST",
        data: "{'SomeText': " + $("#textbox").val() + "}",
        url: "/YourWebService.asmx/MethodName",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: functionToCallWhenSucceed,
        failure: funcitonToCallWhenFailed
});


But why do I want to do that, when I can call the webservice with more simple code like this:

Namespace.WebserviceName.TheMethod(Variable1, Variable2, functionToCallWhenSucceed, funcitonToCallWhenFailed);


I know that the first option is a POST call, but what difference does it make? And isnt the data transferred from the .net 4.0 webservice not serialized in JSON anyway?

I hope you can help me understand the difference between these ways of communicating with a webservice.

Thanks alot, and kind regards,
Lars
Posted

1 solution

"But why do I want to do that, when I can call the webservice with more simple code like this?"
You are quite right: the second code is simpler, but the first call can be made from the browser (it is just Javascript). This means you can consume web services direct from an HTML page. The first method would need to be called from the web server.[Edit] You can AJAX enable the webservice, but this requires the download of a lot of JavaScript (~1700k!) to make the call most end users don't want to do this, the jquery is lighter.

And isnt the data transferred from the .net 4.0 webservice not serialized in JSON anyway?
Not always it depends on your set-up, it can be transferred via a protocol called SOAP, this is much more verbose (a specialised XML Schema) but it does have stronger typing. SOAP vs JSON is a holy-war topic, but each has its place if used correctly. You have to change the serializer to return JSON , the default serialisation is XML.
 
Share this answer
 
v2
Comments
lvq684 4-Apr-11 1:09am    
Thanks alot for your answer!

What do you mean that the first example can be called from Javascript? The second can as well be called from javascript (dont know if that call is somehow wrapped by jquery, but thats what I am using right now on client-side).

Allright, but if you dont setup anything regarding JSON/SOAP serilization, JSON is used by default right?
Keith Barrow 4-Apr-11 4:29am    
I don't think the second example can be called from javascript (to be honest I haven't /wouldn't try): It relies upon proxy classes/code, you wouldn't want these on your machine just to view a website. The first example is jquery and therefore acceptable to users. I have updated my answer for a fuller response!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900