1

I am toying with Ajax-Enabled WCF services. So far I've managed to create and consume my service with a small client application. However while reading the "How-to" page on the msdn website, I've came accross this piece of code :

function Button1_onclick() {
    var service = new SandwichServices.CostService();
    service.CostOfSandwiches(3, onSuccess, null, null);
}

function onSuccess(result) {
    alert(result);
}

and I fail to understand (or even find information about) the null, null parameters when calling the function. 3 is the parameter you want to pass to the service function, onSuccess is the function that is called on a successfull callback but what are those 2 null parameters ?

WizLiz
  • 2,068
  • 5
  • 25
  • 39
  • 1
    does the answer here help it make more sense? http://stackoverflow.com/questions/14128185/where-are-these-extra-parameters-coming-from – rogerdeuce Jan 29 '15 at 14:49
  • Got following from above comment's answer - last 2 parameters refer to `onFailure`, `userContext` respectively. – Arindam Nayak Jan 30 '15 at 13:46
  • I had stumbled upon this answer which confused me even more. Indeed you find that there are much more parameters but still no explication about what they do or how to use them which is what I'm looking for. I've got an idea about onFailure but I don't get the other. – WizLiz Jan 31 '15 at 09:37

2 Answers2

2

The last two parameters refer to onFailure and Usercontext respectively for below mentioned question.

service.CostOfSandwiches(3, onSuccess, null, null);

OnFailure : As the name suggest, if ajax call fails because of network issue, server error, timeout etc.., function passed here as argument will invoked.

UserContext: Generally all ajax call (I mean this[WCF Ajax] is too dependent on ajax call) uses this parameter. Because, there is a main method that call $.ajax, in your case it is Button1_onclick. So upto this point all variables defined will not be available in callbacks ( success or failure), because these are called back when ajax call completes, but to make those call aware of previously defined variable, context option is used to pass them.

Example:

function Button1_onclick() {
    var service = new SandwichServices.CostService();
    var some_var = "someval"; // on successcallback if you are trying to access this, it will show error.
    service.CostOfSandwiches(3, onSuccess, null, null);
}

To use this in success callback , you have to pass that as usercontext object -- like this service.CostOfSandwiches(3, onSuccess, null, some_var);

OnSuccessMethod -

function onSuccess(result,usctx,methodname)
{ 
      if (usctx == "someval")
      {
            alert(result);
      }
}

For more info on context option in general ajax sense , refer this - How to pass context in jquery ajax success callback function

Community
  • 1
  • 1
Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48
0

They are callbacks for sucess, error, and userContext. You can read a bit more here under the "Accessing WCF web services" section.

Aaron
  • 1,361
  • 1
  • 13
  • 30