Calling Web Service from Client-Side JavaScript

来源:互联网 发布:淘宝大衣比较好的店铺 编辑:程序博客网 时间:2024/05/16 14:07

This tip basically helps the developer to call the web service from client-side JavaScript.

 

è First, create a web service project using and give the namespace and the class name as “consumeDnTWebSvc” and write a method called “HelloWorld” as follows:

 

[WebMethod]

public string HelloWorld()

{

return "Test is Successfull...!!!";

}

 

à This is basically a very simple method.

 

è Second, create a web application. Once created the project, download webservice.htc file from this link http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/samples/internet/behaviors/library/webservice/default.asp

 

Note: The program will not work without this htc file.

 

In the .aspx file, copy the following code under the <HEAD> tag:

 

<SCRIPT language="JavaScript">

                                               

function initWebHTC()

{

            //init and create short-cut name for web service

dNtWbSvc.useService("http://YourAddress/WebServiceProjectName/XXX.asmx?WSDL","dNtSvc");

            var iCallID;

            if (dNtWbSvc.dNtSvc)

            {

                                                            //document.getElementById("lblDateTime").innerHTML="Loading..."        

                        iCallID= dNtWbSvc.dNtSvc.callService(gtDnTRslt,"HelloWorld");

            }

}

                       

function gtDnTRslt(result)

{

            //if an error then get error details

            if(result.error)

            {

                        //Pull error info from event.result.errorDetail properties

                        var xfaultcode   = result.errorDetail.code;

                        var xfaultstring = result.errorDetail.string;

                        var xfaultsoap   = result.errorDetail.raw;

                        // Add code to handle specific error codes here

                        //document.getElementById("lblDateTime").innerHTML="Error: " + xfaultcode + " ; " + xfaultstring + " ; " + xfaultsoap;                                                                              

                        alert("Error: " + xfaultcode + " ; " + xfaultstring + " ; " + xfaultsoap);

            }

            else

            {

                                               

                                                //document.getElementById("lblDateTime").innerHTML=result.value;

                        alert(result.value);

                        //clear any selection

                        var sRng;

                        sRng = document.selection.createRange();

                        sRng.execCommand("unselect");

            }

                                   

}

                       

</SCRIPT>

 

Add the following code under <BODY> tag:

 

<body MS_POSITIONING="GridLayout" ondblclick="initWebHTC();">

                       

<DIV id="dNtWbSvc" style="BEHAVIOR: url(http://YourAddress/SecondProjectName/webservice.htc)"></DIV>

 

<form id="frmWebSvcDateTime" method="post" runat="server">

 

<asp:Label backcolor="#99eedd" id="lblDateTime" runat="server" Width="581px" Height="36px">Sample Text Here</asp:Label>

 

</form>

 

</body>
原创粉丝点击