Newb here. I have tried multiple solutions found on SO and other places. This is the one I tried to follow to make sure my function didn't fire until all ajax calls have been made. For some reason, when my updateValues function fires the console log is not returning the data that was updated in my 'first' jQuery each function. However, if I click 'saveButton' again directly after the first time, the console log shows the correct data, as is updated by each function. I've followed all examples I've found but I'm stumped. Thanks in advance!
$("body").on("click",".saveButton",function(){
var first = $(".item");
var i = 0;
first.each(function(){
var $this = $(this);
var thisID = $this.find(".itemID").html();
var itemProperties = {'Status': 'New'};
updateListItem(_spPageContextInfo.webAbsoluteUrl,'TestList',thisID,itemProperties,printInfo,logError);
i += 1;
if(i == first.length) {
updateValues();
}
function printInfo()
{
console.log('UPDATED!');
}
function logError(error){
alert("An error occurred.");
console.log(JSON.stringify(error));
}
});
function printInfo(){
console.log('UPDATED!');
}
function logError(error){
alert("An error occurred.");
console.log(JSON.stringify(error));
}
});
function updateValues(){
var url = _spPageContextInfo.siteServerRelativeUrl+
"/_api/lists/getbytitle('TestList')/items?";
$.ajax({
url: url,
method: 'GET',
beforeSend: function (XMLHttpRequest){
XMLHttpRequest.setRequestHeader('Accept', 'application/json; odata=verbose');
},
success: function( data ) {
console.log(data);
$(".itemTwo").each(function(){
var $this = $(this);
});
}
});
}
function updateListItem(webUrl,listTitle,listItemId,itemProperties,success,failure){
var listItemUri = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/items(" + listItemId + ")";
var itemPayload = {
'__metadata': {'type': "SP.Data.IntakeTESTListItem"}
};
for(var prop in itemProperties){
itemPayload[prop] = itemProperties[prop];
}
updateJson(listItemUri,itemPayload,success,failure);
}
function updateJson(endpointUri,payload, success, error){
$.ajax({
url: endpointUri,
type: "POST",
data: JSON.stringify(payload),
contentType: "application/json;odata=verbose",
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest" : $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "MERGE",
"If-Match": "*"
},
success: success,
error: error
});
}