user3191903
user3191903

Reputation: 245

How to pass additional parameters to ajax call back function?

I am making a ajax call as follows:

util.AjaxCall(url,successCallbackFunction,errorCallbackFunction);

function successCallbackFunction(result){
   //Ajax returns result
}

Everything is working fine, except I have to pass one of my own parameter to be available on 'successCallbackFunction'.

Something like

function successCallbackFunction(result, passedParameter){
}

How can I do that ?

Note: I don't have access to util.AjaxCall source code ( I am not authorized to change that code).

Upvotes: 1

Views: 137

Answers (2)

RienNeVaPlu͢s
RienNeVaPlu͢s

Reputation: 7632

You can use bind:

util.AjaxCall(url,
              successCallbackFunction.bind(this, passedParameter),
              errorCallbackFunction);

The first parameter is the functions scope and in your case propably negligible.

Which will then trigger your successCallbackFunction with an additional (prepended) parameter:

function successCallbackFunction(passedParameter, result){}

Upvotes: 2

user3192244
user3192244

Reputation: 86

Normally when you are sending/receiving Ajax request you exchange data with JSON format. so that you can receive your data as JSON Object.

For example if the line below is the return result via Ajax call

{"firstname":"john","lastname":"doe"}

then you can retreive your data by

function successCallbackFunction(result){
  var obj = jQuery.parseJSON(result);  // if you are using Jquery if not use eval
  // var obj = eval(result);           // this is not recommeneded for security issue.
  var firstname = obj.firstname;
  var lastname = obj.lastname;
}

Upvotes: 0

Related Questions