I am trying, in vain it seems, to be able to pass additional parameters back to the success callback method that I have created for a successful ajax call. A little background. I have a page with a number of dynamically created textbox / selectbox pairs. Each pair having a dynamically assigned unique name such as name="unique-pair-1_txt-url" and name="unique-pair-1_selectBox" then the second pair has the same but the prefix is different.
In an effort to reuse code, I have crafted the callback to take the data and a reference to the selectbox. However when the callback is fired the reference to the selectbox comes back as 'undefined'. I read here that it should be doable. I have even tried taking advantage of the 'context' option but still nothing. Here is the script block that I am trying to use:
<script type="text/javascript" language="javascript">
$j = jQuery.noConflict();
function getImages(urlValue, selectBox) {
$j.ajax({
type: "GET",
url: $j(urlValue).val(),
dataType: "jsonp",
context: selectBox,
success:function(data){
loadImagesInSelect(data)
} ,
error:function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
function loadImagesInSelect(data) {
var select = $j(this);
select.empty();
$j(data).each(function() {
var theValue = $j(this)[0]["@value"];
var theId = $j(this)[0]["@name"];
select.append("<option value='" + theId + "'>" + theValue + "</option>");
});
select.children(":first").attr("selected", true);
}
</script>
From what I have read, I feel I am close but I just cant put my finger on the missing link. Please help in your typical ninja stealthy ways. TIA
****UPDATE**** Nick is a true Ninja. They should invent a new badge for that! His answer below does the trick. As he mentions it's 1.4 specific but I can live with that. Here is my final code that is working for any Ninjas in training (and my future reference):
<script type="text/javascript" language="javascript">
$j = jQuery.noConflict();
function getImages(urlValue, selectBox) {
$j.ajax({
type: "GET",
url: urlValue+ '?callback=?',
dataType: "jsonp",
context: selectBox,
success: jQuery.proxy(function (data) {
var select = $j(this);
select.empty();
$j(data).each(function() {
var theValue = $j(this)[0]["@value"];
var theId = $j(this)[0]["@name"];
select.append("<option value='" + theId + "'>" + theValue + "</option>");
});
select.children(":first").attr("selected", true);
}, selectBox),
error:function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
</script>
this
is Out of ScopeJust so you (or anyone else) understand(s)1, the reason that
this
was undefined inloadImagesInSelect()
in your original example was because that function was under a different scope, and scopes don't "cascade" like that2.When you specified a "context" of
selectBox
in your AJAX call, it set the context (this
) for the functions given to "success" and "error". (It just so happens that they were both anonymous functions.) At this point,this
is defined in both of these functions, as the value ofselectBox
. Now, in the function passed to "success", you callloadImagesInSelect()
. When you call a function, it creates a new scope. In this scope,this
will bewindow
in non-strict mode andundefined
in strict mode.Put Graphically (in Strict Mode3):
$.proxy()
is RedundantThe use of
$.proxy()
in your updated code is redundant. You use$.proxy()
to getthis
to the function that was formerly calledloadImagesInSelect()
, but you moved that function up into "success" anyway (instead of calling it from within "success"). It already has access now to the value ofthis
specified by "context".You could remove the
$.proxy()
wrapper around your "success" function in your updated code and it would still work.I know it's been years since you asked your question, but I hope this helps.
this
variable) still isn't available in the inner function.undefined
withwindow
for non-strict mode.Function.prototype.bind()
in ECMAScript 5.You can use indexValue attribute for passing :
Put this into your $.ajax parameters.
In the success function use it like this
Your $.ajax call and the success function must also be a part of the same object. Put your functions in an object and define them like this
This is what I did, and it also worked fine:
You can add controls and variables to the parameters of your ajax call.
Updated: If you're using jQuery 1.4 use this to simplify things a bit: