/    Sign up×
Community /Pin to ProfileBookmark

JQuery – Sending parameters to callback function

When using the $.ajax() function for sending a request for information from server-side scripting, can you pass parameters to the callback function?

Example – How can I accomplish something like this?

this.request = function(phpURL, arrayToUse, firstTime, startPos, callback){
$.ajax({
type: “POST”,
url: phpURL,
data: buildParameters(firstTime, startPos),
dataType: “xml”,
success: callback(data, arrayToUse)}
});
}

Please note: Taking out ‘arrayToUse’ from ‘callback(data, arrayToUse)’ works.

Thanks,

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@Jeff_MottJan 26.2009 — It would have to go like this.

this.request = function(phpURL, arrayToUse, firstTime, startPos, callback){
$.ajax({
type: "POST",
url: phpURL,
data: buildParameters(firstTime, startPos),
dataType: "xml",
success: [color=red]function (data, textStatus){
YourFunction(data, arrayToUse);
}[/color]
});
}
Copy linkTweet thisAlerts:
@golfinusaauthorJan 26.2009 — Yeah, I realize that is one way of doing it, but the reason I was doing it by passing in callback is because my goal is to use different callback functions but use the same request function.

I suppose I might be able to use the DOM to store some variables for me, then access them directly from the DOM inside of the callback function, but I was hoping there might be a cleaner route.

Anymore ideas?

Note: Just thought of this... where you say 'YourFunction(data, arrayToUse)', could that actually be callback where callback is defined as:

tools.request("phpPage.php", array, true, 1, updatePage)

where updatePage is:

var updatePage = new function(data, arrayToUse){

//Code here

}

Thanks
Copy linkTweet thisAlerts:
@Jeff_MottJan 26.2009 — Note: Just thought of this... where you say 'YourFunction(data, arrayToUse)', could that actually be callback...[/quote]Yes. Exactly.
Copy linkTweet thisAlerts:
@golfinusaauthorJan 26.2009 — Thanks a ton!

I'll give that a try.

-C
Copy linkTweet thisAlerts:
@golfinusaauthorJan 27.2009 — Okay, so I tried that, and it didn't work. I would post a link, but it's not live atm.

Here is what I tried:

<i>
</i>this.request = function(phpURL, dType, arrayToUse, firstTime, startPos, callback){
$.ajax({
type: "POST",
url: phpURL,
data: buildParameters(firstTime, startPos),
dataType: dType,
success: function (data, textStatus){
callback(data, arrayToUse);
}
// I've also tried this:
//success: function(data, textStatus, container){...
});
}


<i>
</i>/* Callback function */
this.updatePage = function(data, container){
//Code here
}


But, when it gets into my callback function, container is undefined/null. I do know that arrayToUse is populated with a good value when inside 'this.request', it's just not passing it to my callback function.

Here is what I'm trying to do: I'm trying to make 'this.request' generic so that I can use it throughout the entire code base. So, when I initially load the page, it will do 'a', but when I execute it when a user does an 'onclick' it will call a different url with a different data type using a different container (array).

Thank you for the help.

-C
Copy linkTweet thisAlerts:
@Jeff_MottJan 27.2009 — I checked and I tested, but I don't see any reason why the arrayToUse variable wouldn't pass its value, and there's no problem in the code you posted, so the problem must be elsewhere. You'll need to post the entire page and JS, preferably as attachments to a post.
×

Success!

Help @golfinusa spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...