Calling asp.net page method from javascript not working
Asked Answered
C

4

9

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.

Chorizo answered 18/5, 2012 at 18:41 Comment(0)
L
2

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.

Legislative answered 18/5, 2012 at 19:5 Comment(5)
Hi i tried it with [System.Web.Script.Services.ScriptMethod] but i got the error "PageMethods is not defined" in firebug console.Chorizo
Did you add ScriptService as well? If the class isn't defined anymore it's because the javascript file that defines the class interface isn't loading. Try looking at the network tab and see if anything is 404'ing on you.Legislative
I got compile error. "Attribute 'System.Web.Script.Services.ScriptService' is not valid on this declaration type. It is only valid on 'class, interface' declarations. "Chorizo
I use webmethond and Scriptmethod, at firebug, i can see the response is {"d":"Yes this is working"} . It seems the page assign the value to "d" but i dont' know how to retrieve the value from d.Chorizo
This is how asp.net ajax web services send information, they wrap the response in an object property called "d". You can read more about it here: encosia.com/never-worry-about-asp-net-ajaxs-d-again . Glad it's working for you though.Legislative
I
3

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.

Impromptu answered 16/9, 2012 at 17:44 Comment(0)
L
2

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.

Legislative answered 18/5, 2012 at 19:5 Comment(5)
Hi i tried it with [System.Web.Script.Services.ScriptMethod] but i got the error "PageMethods is not defined" in firebug console.Chorizo
Did you add ScriptService as well? If the class isn't defined anymore it's because the javascript file that defines the class interface isn't loading. Try looking at the network tab and see if anything is 404'ing on you.Legislative
I got compile error. "Attribute 'System.Web.Script.Services.ScriptService' is not valid on this declaration type. It is only valid on 'class, interface' declarations. "Chorizo
I use webmethond and Scriptmethod, at firebug, i can see the response is {"d":"Yes this is working"} . It seems the page assign the value to "d" but i dont' know how to retrieve the value from d.Chorizo
This is how asp.net ajax web services send information, they wrap the response in an object property called "d". You can read more about it here: encosia.com/never-worry-about-asp-net-ajaxs-d-again . Glad it's working for you though.Legislative
K
1

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.

Klockau answered 26/3, 2014 at 18:6 Comment(0)
F
0

from what i remember, you just need 3 params in your call(your param, onsuccess and onfailure). did you try using PageMethods.TestMethod("test parameter", OnCallSumComplete, OnCallSumError);

Firstly answered 18/5, 2012 at 18:54 Comment(1)
yes, i did, but still get "null" message. thanks for the response.Chorizo

© 2022 - 2024 — McMap. All rights reserved.