DCSIMG
How to: Calling Web Services methods from Client Script in ASP.NET AJAX - Maor's Blog

How to: Calling Web Services methods from Client Script in ASP.NET AJAX

My previos post explained the way to expose a web service to client script in Ajax. This post will introduce the "How to" call the web service's method from the client script.

Our web service is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
namespace MySamples.BL
{
[WebService(Namespace = "http://tempuri.org/")]
[ScriptService]
public class MyWebService : System.Web.Services.WebService
{
[WebMethod]
public string PrintString(String input)
{
return input;
}
[WebMethod]
public string SayHello()
{
return "Hello";
}
}
}

Calling a Web service method from script is asynchronous. Succeeded callback function must provided to get a return value or to determine when the request has returned. This function is invoked when the request has finished successfully with return value (as a parameter) from the Web method call. Failed callback function can also provided to handle errors.

Based on the previous post, this is what you should do in the client script.

The script is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// This function calls the Web service method 
// without parameters and
// passes the event callback function.
function SayHello()
{
MySamples.BL.MyWebService.SayHello(SucceededCallback);
}

// This function calls the Web service method
// passing simple type parameters and the
// callback function 
function PrintString(str)
{
    MySamples.BL.MyWebService.PrintString(str,SucceededCallback);
}


// This is the callback function invoked if the Web service succeeded.
function SucceededCallback(result, eventArgs)
{
    var wsResult= document.getElementById("wsResult");
    wsResult.innerHTML = result;
}

Thats all!

Published 11 May 2007 11:39 PM by Maor David-Pur
תגים:, ,

Comments

# Maor David's Blog : How to: Expose Web Services to Client Script in AJAX said on 13 May, 2007 01:59 PM
PingBack from http://blogs.microsoft.co.il/blogs/maordavid/archive/2007/05/10/How-to_3A00_-Expose-Web-Services-to-Client-Script-in-AJAX.aspx
# http://blogs.microsoft.co.il/blogs/maordavid/archive/2007/05/11/How-to_3A00_-Calling-Web-Services-methods-from-Client-Script-in-ASP.NET-AJAX.aspx said on 26 March, 2008 10:54 AM

Pingback from  blogs.microsoft.co.il/.../How-to_3A00_-Calling-Web-Services-methods-from-Client-Script-in-ASP.NET-AJAX.aspx

# Maor David said on 17 May, 2008 04:54 PM

Last week it was exactly one year since I started blogging, so this is my blog first birthday!! Come and read about the blog's statistics, top posts and more.

# Maor David said on 17 May, 2008 04:55 PM

Last week it was exactly one year since I started blogging, so this is my blog first birthday!! Come and read about the blog's statistics, top posts and more.

# Maor David said on 17 May, 2008 04:58 PM

Last week it was exactly one year since I started blogging, so this is my blog first birthday!! Come and read about the blog's statistics, top posts and more.

# ASP.NET 3.5, AJAX and WebServices « Private: MY Note said on 17 October, 2008 03:33 AM

Pingback from  ASP.NET 3.5, AJAX and WebServices   « Private: MY Note

# ASP.NET 3.5, AJAX and WebServices | Private: My Note said on 25 October, 2009 08:16 PM

Pingback from  ASP.NET 3.5, AJAX and WebServices | Private: My Note

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above:

Search

Go

This Blog

News

    RSS

     

    Connect with Me

    Maor's Facebook profile  Follow Maor on Twitter  Maor's profile on Linkedin  Maor in FriendFeed 
           

Syndication