Using variables in ajax callbacks seems to work but I don't understand why. Here is an example. Can someone explain why the variable, in this case subSiteUrl, set outside of the callback is correct?
I would expect the variable to be unreliable as the loop likely progressed prior to the callback occurring.
function getSubWebProjInformation() {
$.each(subWebsArray, function() {
var subSiteUrl = this.Url;
var targetUrl = this.Url + "/_vti_bin/lists.asmx";
var listName = "Project Information";
var soapEnv = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
" <soap:Body>" +
" <GetList xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\">" +
" <listName>" + listName + "</listName>" +
" </GetList>" +
" </soap:Body>" +
"</soap:Envelope>";
$.ajax({
cache: false,
url: targetUrl,
type: "POST",
dataType: "xml",
data: soapEnv,
contentType: "text/xml; charset=utf-8",
beforeSend: function(xhr) {
xhr.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/GetList");
},
complete: function(msg) {
if (msg.status == 200) {
var projFieldsArray = [];
$(msg.responseXML).find("Field").each(function() {
var staticName = $(this).attr("StaticName");
var displayName = $(this).attr("DisplayName");
var isFromBaseType = $(this).attr("FromBaseType") === "TRUE";
if (!isFromBaseType && staticName && staticName != "PermMask" || staticName === "Title") {
var f = {};
f.displayName = displayName;
f.staticName = staticName;
projFieldsArray.push(f);
}
});
updateSubWebObjectWithProjFields(subSiteUrl, projFieldsArray);
} else {
//Failure
var errorCode = $(msg.responseXML).find("errorcode").text();
var errorString = $(msg.responseXML).find("errorstring").text();
if (errorString.length === 0) {
errorString = $(msg.responseXML).find("faultstring").text();
}
errorString = errorString.replace(/(\r\n|\n|\r)/gm, "");
showStatusBar("Oh no! " + errorString);
}
},
});
});
}
function updateSubWebObjectWithProjFields(subSiteUrl, projFieldsArray) {
console.log(subSiteUrl);
$.each(projFieldsArray, function() {
console.log(this.displayName + ": " + this.staticName);
});
}