C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Call an ASP.NET web service from JavaScript


Posted Date: 18 Jun 2007    Resource Type: Code Snippets    Category: Javascript

Posted By: Abhishek Arya       Member Level: Diamond
Rating:     Points: 10



This sample code shows how to call a webservice from client side java script.


<%@ WebService Language="C#" class=MyMath %>

using System;
using System.Web.Services;

public class MyMath {

[WebMethod]
public int add(int a, int b)
{
return a + b;
}

[WebMethod]
public int subtract(int a, int b)
{
return a - b;
}

}

A simple test page:







Enter first Value

Enter Second value













Responses

Author: ashok    23 Aug 2008Member Level: Bronze   Points : 2
but before accessing through java script you should enable the access at server side as shown in below example

To enable an .asmx Web service to be called from client script in an ASP.NET Web page, you first add a ScriptManager control to the page. You reference the Web service by adding an asp:ServiceReference child element to the ScriptManager control and setting its path attribute to point to the Web service. The ServiceReference object instructs ASP.NET 2.0 AJAX Extensions to generate a JavaScript proxy class for calling the specified Web service from client script.

The following example shows how to enable a Web service named SimpleWebService.asmx to be called from script in an ASP.NET Web page.



path="~/WebServices/SimpleWebService.asmx" />



The ServiceReference object can reference a Web service only in the same domain. The Web service path can be relative, application relative, domain relative, or absolute. For absolute paths, you must make sure that the path is in the same domain.

When a page that contains this ScriptManager control is rendered, it creates a JavaScript proxy class for the SimpleWebService.asmx Web service. The proxy class has methods that correspond to each Web method in the SimpleWebService.asmx service. The page also contains JavaScript proxy classes that correspond to server data types that are used as input parameters or return values for the Web service methods. This enables you to write client script that initializes these parameters, and to pass them to the method call.

The InlineScript property of the ServiceReference object specifies how the JavaScript proxy class is included in the page. If InlineScript is set to false (the default), the proxy script is obtained by making a separate request. This option is better when multiple pages reference the same service and when browser caching is enabled.

If InlineScript is set to true, the proxy class script is included as an inline script block in the page. This can improve performance by reducing the number of network requests. This is especially true if there are many service references in the page and other pages do not reference the same service. If InlineScript is set to true, you must use a relative path. If the path is domain-relative, it must refer to the same Web application.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Script to set default home page or add to favorites in browser
Previous Resource: Convert from string to datetime format using java script.
Return to Discussion Resource Index
Post New Resource
Category: Javascript


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

doors in nj

Contact Us    Privacy Policy    Terms Of Use