Trying to call a C# Web service method from Javascript. Cannot get it to work.

Posted on 2009-02-15
Last Modified: 2013-12-17
I am trying to call a Web Service method from javscript function. I have managed to do it before, this time it will not work. i cannot remember if I need to make modifications to the web.config file. Please any assistance would be greatly appreciated. Code example is below.
Here is the .aspx code:


     <script src="MyTester.js" type="text/javascript"></script>


<form id="form1" runat="server">

    <input id="Button1" type="button" value="MyTester" onclick = "callWebService()" />


Here is the Javascript code:

function callWebService()




Here is the codebehind .cs with the Web Service method that we are trying to call:


    public static void FileDeletion()


        int one = 1;

        int two = 2;

        int result = one + two;


Open in new window

Question by:metta0_3
    LVL 10

    Expert Comment

    PageMethods are use when you have a "PageMethods" in your aspx code-behind page. If you want to access a web-services you need to add the asmx ref to the scriptmanager.

    Take a look at these video tutorial from
        public string SayHello(String inStr) {
            return "Hello : " + inStr + "; Thanks for visiting.";
    aspx page
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            <asp:ServiceReference Path="SimpleService.asmx" />
    function Button1_onclick() {
    ret = SimpleService.SayHello(document.getElementById('Text1').value, OnComplete, OnTimeOut, OnError);

    Open in new window


    Author Comment

    Thanks for your advice. That is very useful. All I really want to do is call an method from Javascript. There must be a way to do this without the need to create web services. It just seems like alot for something so small. Creating web services just to call one method in code behind from Javascript.

    Any idea on alternative approach.
    LVL 10

    Accepted Solution

    I've just answer a similar question few days ago.

    You will need to use the "PageMethod"
    Check this out. This will explain how to call a " method" from your javascript page.

    Post ref:


    Author Closing Comment

    Thank you. Just what I was looking for:

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now