Calling server-side script from AJAX

Posted on 2009-04-15
Last Modified: 2013-11-26
I am trying to call server-side script through javascript but I encounter following error,

PageMethods is undefined.

Please see attched file,

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

    Namespace="System.Web.UI" TagPrefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="" >

<head runat="server">

    <title>Untitled Page</title>



<body onload="CallSum()" >

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

   <asp:scriptmanager ID="Scriptmanager1" runat="server" EnablePageMethods="true"></asp:scriptmanager>

   <script language="javascript" type="text/javascript">

    function CallSum() 


        //Get the controls

        var txt1 = document.getElementById('box1');

        var txt2 = document.getElementById('box2');

        var txtresult = document.getElementById('answer');

        PageMethods.Sum(txt1.value, txt2.value, OnCallSumComplete, OnCallSumError, txtresult);



    function OnCallSumComplete(result,txtresult,methodName)


        //Show the result in txtresult

        txtresult.value = result;


    function OnCallSumError(error,userContext,methodName)


        if(error !== null) 






        <asp:TextBox ID="box1" runat="server" Style="z-index: 100; left: 144px; position: absolute;

            top: 120px">2</asp:TextBox>


        &nbsp; &nbsp;&nbsp;

        <asp:TextBox ID="box2" runat="server" Style="z-index: 101; left: 320px; position: absolute;

            top: 120px">5</asp:TextBox>


        <asp:TextBox ID="answer" runat="server" Style="z-index: 103; left: 264px; position: absolute;

            top: 200px"></asp:TextBox>





 Public Shared Function Sum(ByVal x As Integer, ByVal y As Integer) As Integer

        Return x + y

    End Function

Open in new window

Question by:siddiqishahid
    LVL 12

    Accepted Solution

    Define the WebMethod attribute and it has to be marked static for the ScriptManager to see it

    public partial class _Default : System.Web.UI.Page

      public static int Sum (int x, int y)
        return DateTime.Now.ToString();

    Author Comment

    Now I am getting the result but also I am getting javascript popup message and it says, "Permission denied"

    It points to PageMethods line when I view the source. Any suggestions?

    LVL 12

    Expert Comment

    i think it has to do with calling CallSum from the onload event of the body.  Remove it from there, and place a simple <input type=button onclick="CallSum();" /> element on the page. Tell me if thatworks correctly.

    Author Comment

    Thanks for you help!!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

          Install BugTracker on Windows 2008 server Step 1:  Install windows 2008 server 32 bit OS and configure IIS. Step 2:  Install SQL server ( SQL server 2005 or SQL server 2005 Express edition. The installer for 2008  version isn’t very f…
    Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
    This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
    Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

    737 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

    20 Experts available now in Live!

    Get 1:1 Help Now