Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling asp.net page method from javascript not working

Hi I am calling a simple page method from javascript , here is my code at markup

 function OnCallSumComplete(result, userContext, methodName) {             
            alert(result);
 }
 function OnCallSumError(error, userContext, methodName) {
     if (error !== null) {
         alert(error.get_message());
     }
 }
 function test(){
     var contextArray = "";
     PageMethods.TestMethod("test parameter", OnCallSumComplete, OnCallSumError,  contextArray);
 }

 <asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server" />

at cs

 [System.Web.Services.WebMethod]
 public static string TestMethod(string para)
 {

    return "Yes this is working";
 }

the alert show the result and it says "null". I check firebug and i don't see error from console.

If i change the TestMethod to

 [System.Web.Services.WebMethod]
 public static string TestMethod()
 {
    return "Yes this is working";
 }

And PageMethod to

 PageMethods.TestMethod( function (response) { alert(response);  } );

It shows the correct response as "Yes this is working". However, i need to pass parameter to the function. Do i miss anything?

Thanks for help.

like image 614
windforceus Avatar asked May 18 '12 18:05

windforceus


3 Answers

I think the main problem is with the assembly you are using for ScriptManager.

<asp:ScriptManager ID="ScriptManager1" 
                   EnablePageMethods="true" 
                   runat="server" />

To resolve your problem use in Webconfig -

<pages>
  <controls>
    <add tagPrefix="ajax" 
         namespace="System.Web.UI" 
         assembly="System.Web.Extensions, 
                   Version=1.0.61025.0, 
                   Culture=neutral, 
                   PublicKeyToken=31bf3856ad364e35"/>
  </controls>
</pages>

and in your .aspx page use following lines -

<ajax:ScriptManager ID="ScriptManager1" 
                    EnablePageMethods="true" 
                    runat="server" />

Hope this will help you to resolve your problem.

like image 140
Shiv Mohan Saxena Avatar answered Nov 13 '22 12:11

Shiv Mohan Saxena


I think you have to use [ScriptMethod] instead of or in addition to [WebMethod] in order to have asmx methods available via javascript calls. The reason why it might work without taking a parameter is because the request doesn't have to parse anything in order to process the method.

Try it with [ScriptMethod] (and possibly [ScriptService] on your class definition) and see if that makes a difference.

like image 40
RTigger Avatar answered Nov 13 '22 14:11

RTigger


The problem is that on your Web.config you need to have a module (IHttpModule) enabled: ScriptModule-4.0. This is enabled by default, but you may have removed it. Look for it in the machine-wide Web.config file, if you are curious, and see if it was removed from your local Web.config. Its declaration should be under system.webServer/modules (for IIS >= 7) and system.web/httpModules for Visual Studio's built-in web server or IIS < 7.

like image 23
Ricardo Peres Avatar answered Nov 13 '22 13:11

Ricardo Peres