Link to home
Start Free TrialLog in
Avatar of siddiqishahid
siddiqishahid

asked on

Calling server-side script from AJAX

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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    
</head>
<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) 
        {
            alert(error.get_message());
        }
    }
 
</script>
        <asp:TextBox ID="box1" runat="server" Style="z-index: 100; left: 144px; position: absolute;
            top: 120px">2</asp:TextBox>
    <div>
        &nbsp; &nbsp;&nbsp;
        <asp:TextBox ID="box2" runat="server" Style="z-index: 101; left: 320px; position: absolute;
            top: 120px">5</asp:TextBox>
        &nbsp;&nbsp;
        <asp:TextBox ID="answer" runat="server" Style="z-index: 103; left: 264px; position: absolute;
            top: 200px"></asp:TextBox>
    </div>
    </form>
</body>
</html>
 
 
 
 Public Shared Function Sum(ByVal x As Integer, ByVal y As Integer) As Integer
        Return x + y
    End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of wht1986
wht1986
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of siddiqishahid
siddiqishahid

ASKER

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?

Thanks
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.
Thanks for you help!!