Calling a web service from JavaScript was easy task earlier you need to create a proxy class for JavaScript and then you need to write lots of java script but with Microsoft ASP.NET Ajax you can call web services from JavaScript very easily with some line of JavaScript and even better you can also handle the errors if web service failed to return result. Let’s create a simple hello world web service which will print Hello world string and then we will call that web service into JavaScript. Following is a code for web service.
namespace DotNetJapsSocial { /// <summary> /// Summary description for HelloWorld /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. [System.Web.Script.Services.ScriptService] public class HelloWorld : System.Web.Services.WebService { [WebMethod] public string PrintHelloWolrdMessage() { return "Hello World"; } } }
Now lets create three functions in JavaScript CallWebService,ReuqestCompleteCallback and RequestFailedCallback. Following is the code for that.
<script type="text/javascript"> function CallWebService() { DotNetJapsSocial.HelloWorld.PrintHelloWolrdMessage(ReuqestCompleteCallback, RequestFailedCallback); } function ReuqestCompleteCallback(result) { // Display the result. var divResult = document.getElementById("divMessage"); divResult.innerHTML = result; } function RequestFailedCallback(error) { var stackTrace = error.get_stackTrace(); var message = error.get_message(); var statusCode = error.get_statusCode(); var exceptionType = error.get_exceptionType(); var timedout = error.get_timedOut(); // Display the error. var divResult = document.getElementById("Results"); divResult.innerHTML = "Stack Trace: " + stackTrace + "<br/>" + "Service Error: " + message + "<br/>" + "Status Code: " + statusCode + "<br/>" + "Exception Type: " + exceptionType + "<br/>" + "Timedout: " + timedout; } </script>
Now Let’s Create div which will print message and a button to call web service like following.
<div> <input type="button" value="Call Web Service" onclick="CallWebService();"/> </div> <div id="divMessage"> </div>
Hope this will help you..Happy coding..
Thanks You for give a such a great information about how to work web service and code for JavaScript with asp.net Ajax.
ReplyDeleteHi, thanx.please help me out.
ReplyDeletemy js isnt recognizing my webservice method. i keep getting
Microsoft JScript runtime error: Object expected
this how im calling it,
AutoComplete.PrintHelloWolrdMessage(ReuqestCompleteCallback, RequestFailedCallback);
AutoComplete is the name of my class.and i have no namespace. Now this is what i have in my scriptmanager
which is sitting in my masterpage
and in another page in my site i use an ajaxToolkit:AutoCompleteExtender with ServicePath="../AutoComplete.asmx" and there it works fine. so why is it not recognized here??
thank in advance. Judy
Thanks for posting nice article.
ReplyDeleteI am trying to return an object instead on string from webmethod, how to get values from 'result' in this scenario?
Thanks and regards,
Ravi
Hello,
ReplyDeleteThanks for article!
Well, If you are returning serialized object then only its possible.
ReplyDeleteyou forgot to mention that you need to add it to your scriptmanager:
ReplyDelete<asp:ScriptManager runat="server" ID="scriptManager">
<Services>
<asp:ServiceReference path="~/myWebService.asmx" />
</Services>
hey guys i am facing an issue what if in you code DotNetJapsSocial is undefined as m facing this issue in my call plz do reply
ReplyDelete@00b6f67b554d56d6a6bba706e6ad2fde Can you send me code what you have written?
ReplyDelete