最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can we pass multiple parameters to onSuccess method of PageMethod? - Stack Overflow

programmeradmin5浏览0评论

I'm calling PageMethod "SameMethod" from javascript method "caller" so that I can get some values from DB. After I get values, control is continuing in "onSuccess" method. Problem is that I need to use some variable values ("importantValue") from javascript method "caller" in "onSuccess" method.

 
function caller(){
    var importantValue = 1984;   
    PageMethod.SomeMethod(param1,..., onSuccess, onFailure)
}

onSuccess method should be something like this:

function onSuccess(pageMethodReturnValue, importantValue ){

}

Is it possible and, if it is, how to pass multiple parameters (besides return values of page method) to "onSuccess" method of PageMethod?

Thanks for help

I'm calling PageMethod "SameMethod" from javascript method "caller" so that I can get some values from DB. After I get values, control is continuing in "onSuccess" method. Problem is that I need to use some variable values ("importantValue") from javascript method "caller" in "onSuccess" method.

 
function caller(){
    var importantValue = 1984;   
    PageMethod.SomeMethod(param1,..., onSuccess, onFailure)
}

onSuccess method should be something like this:

function onSuccess(pageMethodReturnValue, importantValue ){

}

Is it possible and, if it is, how to pass multiple parameters (besides return values of page method) to "onSuccess" method of PageMethod?

Thanks for help

Share Improve this question asked Dec 9, 2010 at 12:34 JankoJanko 931 gold badge2 silver badges10 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 13

Pass your importantValue as an additional parameter when calling the PageMethod. (this is usually called the context parameter if you are searching online for more info)

function caller(){
    var importantValue = 1984;   
    PageMethod.SomeMethod(param1,..., onSuccess, onFailure, importantValue)
}

Then you can access the value in the onSuccess callback as follows:

function onSuccess(pageMethodReturnValue, context, methodName){
    // context == 1984
}

Update to explain onSuccess parameters for @JacksonLopes There is a good description on the aspalliance website in an article by Suresh Kumar Goudampally

The important bit (modified to use my parameter names) is:

The success call back method has three parameters:

  • pageMethodReturnValue - Returns the output of the page method.
  • context - This is used to handle different logic when single callback is used for multiple page method requests. We can also pass an array of values as the context parameter.
  • methodName - This parameter returns the name of page method called.

You could use an anonymous function

PageMethod.SomeMethod(param1,..., function(){onSuccess(foo, importantValue)}, onFailure)
发布评论

评论列表(0)

  1. 暂无评论