Within a jQuery ajax function data supplied to the callback function on success is defined with
success: function (data) { ...
but this makes JSLint unhappy ("Don't make functions within a loop").
If I follow the suggestion in http://stackoverflow.com/questions/3037598/how-to-get-around-the-jslint-error-dont-make-functions-within-a-loop, Firebug complains that "data is not defined" and the callback function fails.
Example:
Prior to $(document).ready(function(){
function ajaxSuccess() {
return function (data) {
alert (data);
};
}
Within $(document).ready(function(){
$.ajax({
type: "POST",
url: "some-url-here",
data: ({ "foo" : "bar" }),
success: ajaxSuccess(data)
});
results in "data not defined" error.
But if I change it to
$.ajax({
type: "POST",
url: "some-url-here",
data: ({ "foo" : "bar" }),
success: function (data) {
ajaxSuccess(data);
}
});
then everything is hunky-dory -- but now I'm back where I started as far as JSLint is concerned.
Assuming I want to pass muster with JSLint, how do I get ahold of data
returned by url
and pass it on to the function in question?