Click here to Skip to main content
15,881,730 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
I am using PageMethods to call server side method from client side but I am getting a problem that server side method is calling only if, I am calling the method from source part, if i am making a javascript file with the same javascript code and adding the link into the page then it is not working, only PageMthods is not working but all other thing of javascript file is working fine.

Please suggest how would I resolve this issue.

Thanks
Prafulla
Posted
Updated 14-Feb-19 22:44pm
Comments
Ankur\m/ 4-Apr-13 5:26am    
My first guess would be - the URL isn't correct. Can you show your javascript code which is calling the pagemethod?
JoCodes 14-Oct-13 12:53pm    
post your code which you tried?
sri senthil kumar 4-Apr-13 8:23am    
Check this url for basic page method call syntax
http://witstuner.blogspot.in/2013/02/different-ways-of-ajax-call-in-asp.html

Also post your code so that we could help you...

try this.. :)

Javascript


JavaScript
function GetName() {

    $.ajax({
        type: "POST",
        url: "Default.aspx/GetData", //Pagename/Functionname
        contentType: "application/json;charset=utf-8",
        data: {},//data
        dataType: "json",
        success: function (data) {

           alert(data.d);

        },
        error: function (result) {

         alert("error")

        }
    });
}


HTML

HTML
<span onclick="GetName()">Call Web Method</span>


C#

C#
[WebMethod]
      public static string GetData()
      {
          string name= "My name is Nirav Prabtani";

          return name;
      }
 
Share this answer
 
Hi Friend,

It should not affect weather keeping script in page level or in script file.
Check once weather you gave right file or any other syntax.

it is good to find the error with Firebug..!
 
Share this answer
 
Below given code worked for me.

.cs -


using Microsoft.AspNet.FriendlyUrls;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [System.Web.Services.WebMethod(EnableSession = true)] 
    [System.Web.Script.Services.ScriptMethod(UseHttpGet = false, ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
    public static string yourmethod1()
    {
         return "Allow user";
    }
}


apsx.cs Page -


<%@ Page Language="C#" AutoEventWireup="false" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!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></title>

     <script type="text/javascript">

        function GreetingsFromServer()
        {
            PageMethods.yourmethod1(OnSuccess, OnError)
        }
        function OnSuccess(response)
        {
            alert(response);
        }
        function OnError(error) 
        {
            alert(error);
        }
    </script>

</head>


<body>

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

    <div>
            <asp:ScriptManager runat="server" EnablePageMethods="true"  EnablePartialRendering="true"  > </asp:ScriptManager>

            <input id="Button1" type="button" value="button" onclick=" return GreetingsFromServer();" />

    </div>

    </form>
</body>
</html>


Web.conf -



<configuration>

  <appSettings>
           <add key="owin:AutomaticAppStartup" value="false" />
  </appSettings>

    <system.web>

            <compilation debug="true" targetFramework="4.5.2" />

            <httpRuntime targetFramework="4.5.2" />

            <authorization>
                       <allow users="*"/>
            </authorization>

    </system.web>

</configuration>
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900