Click here to Skip to main content
11,796,394 members (69,265 online)
Click here to Skip to main content

Web Service and Script Service

, 26 Jan 2013 CPOL 25.1K 343 15
Rate this:
Please Sign up or sign in to vote.
Web service and script service

Most of you are familiar with writing a web service using C#. Visual Studio provides the project template which easily creates a web service for you.

I’m sure you may know how to call this web service using C#. But do you know how to call the web service using client side scripts such as JavaScript? There are various ways to do this. I will demonstrate this using jQuery$.ajax() function.

function callSvc() {
        type: "POST",
        url: "Service1.asmx/HelloWorld",
        data: "",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data, status) { alert(data); },
        error: function(request, status, error) { alert(request); alert(status); alert(error); }

When you call the HelloWorld() service, by default you will get “500 – Internal server error” error message.

This error is raised because you haven’t configured this service to access using client side scripts. When you check the responseText , you will notice that it says “Only Web services with a [ScriptService] attribute on the class definition can be called from script.

To resolve this, you have to configure your web service with [ScriptService] attribute.

public class Service1 : System.Web.Services.WebService

When adding [ScriptService] attribute to the web service, it gets automatically handled by the ScriptHandlerFactory and creates a JavaScript proxy class for the web service. That means, you will be able to call the web service from the client side script in the same way as you call it from the code behind. You can view the generated proxy class by using the following command:


Compare the generated proxy class with the service description (http://<URL>/Service1.asmx?WSDL) and let me know what you noticed.


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


About the Author

Tharaka MTR
Technical Lead Eyepax IT Consulting (Pvt) Ltd.
Sri Lanka Sri Lanka
Having more than 9 year hands-on industry experience in software development
Responsible for designing, implementing and managing complex software systems with stringent up-time requirement.

Visit my blog

You may also be interested in...

Comments and Discussions

Questionwell done Pin
wshcdr4-Sep-13 16:25
memberwshcdr4-Sep-13 16:25 
AnswerRe: well done Pin
Tharaka MTR4-Sep-13 22:50
professionalTharaka MTR4-Sep-13 22:50 
QuestionThe zipped solution worked Pin
fdogo10-Jan-13 17:15
memberfdogo10-Jan-13 17:15 
AnswerRe: The zipped solution worked Pin
Tharaka MTR10-Jan-13 17:30
memberTharaka MTR10-Jan-13 17:30 
QuestionCan't use the script just out of the block Pin
Thanasis Ioannidis8-Jan-13 13:16
memberThanasis Ioannidis8-Jan-13 13:16 
AnswerRe: Can't use the script just out of the block Pin
Tharaka MTR10-Jan-13 6:55
memberTharaka MTR10-Jan-13 6:55 
GeneralRe: Can't use the script just out of the block Pin
Thanasis Ioannidis22-Jan-13 3:49
memberThanasis Ioannidis22-Jan-13 3:49 
GeneralRe: Can't use the script just out of the block Pin
Tharaka MTR22-Jan-13 7:24
memberTharaka MTR22-Jan-13 7:24 
No, we no need the script manager control. But for to enable the [ScriptService]
you have to add the "System.Web.Services.dll".

for the client side, we just need jQuery.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151002.1 | Last Updated 26 Jan 2013
Article Copyright 2013 by Tharaka MTR
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid