Thursday, May 23, 2024
Homepage · c#
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  6] [ 4]  / answers: 1 / hits: 24061  / 12 Years ago, fri, may 18, 2012, 12:00:00

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.


More From » c#

 Answers
80

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.


[#85487] Thursday, May 17, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
valentinam

Total Points: 166
Total Questions: 117
Total Answers: 81

Location: Puerto Rico
Member since Sun, Jun 27, 2021
3 Years ago
;