jquery - What does "this" refer to in the following javascript? -
disclaimer: asking specific use of this
, not this
used in general. so, please no google/copy/paste speed answers (:
i have javascript/jquery code below:
var req = {}; function getdata() { var frompage = 0; var topage = 1; req = $.ajax({ url: "/_vti_bin/lists.asmx", type: "post", datatype: "xml", data: xmldata, complete: onsuccess, error: function (xhr, ajaxoptions, thrownerror) { alert("error: " + xhr.statustext); alert(thrownerror); }, contenttype: "text/xml; charset=\"utf-8\"" }); req.frompage = frompage; req.topage = topage; } function onsuccess(resp) { alert(this.frompage); }
i find pretty confusing code using frompage
in 2 places (sorry not code).
does this
refer var declared inside of getdata
or 1 part of req
object? or maybe else entirely....
any appreciated.
according jquery.ajax
documentation:
the reference within callbacks object in context option passed $.ajax in settings; if context not specified, reference ajax settings themselves.
in other words, since didn't set context
option, this
options object {...}
passed parameter $.ajax
.
the code posted seems wrong: reads frompage
wrong object. work if set frompage
on options object instead:
req = $.ajax({ //other options here... frompage: frompage });
Comments
Post a Comment