问题
I am trying to use page methods in my asp.net page. I have enable page methods set to true on the script manager, the webmethod attribute defined on the method, the function is public static string, I know the function works because when I run it from my code behind it generates the expected result, but when I call it via page method in my result function the result is always alerted as undefined. If I use fiddler it doesn't even look like there is additional traffic or a new request created. I'm running the site on port 82 if that makes a difference. I'm at a loss here. Can someone give me some pointers?
回答1:
In your PagesMethods call, remove the parentheses from the callback and error functions:
PageMethods.getAdCodeInfo(value, onSuccess, onError)
onSuccess
and onError
are basically variables that point to the functions. So you don't need parentheses for variable names.
回答2:
OK, stupid me. Here is some code.
function getName()
{
var ddlAdCodes=$get('<%=ddlAdCodes.ClientID %>');
var value=ddlAdCodes.options[ddlAdCodes.selectedIndex].value;
//alert(value);
PageMethods.getAdCodeInfo(value,onSuccess(),onError());
}
function onSuccess(result)
{
alert(result);
}
function onError(error)
{
alert("error "+error);
}
来源:https://stackoverflow.com/questions/223308/asp-net-page-methods-returning-undefined