Skip to main content
2 of 4
added 678 characters in body

ctx is a global object,

Since JavaScript passes objects by reference the ctx inside your success function is not a locally scoped variable.

So after you initiated the Ajax call (its asynchronous!), your CSR code immediatly continues, and does all sort of things with the global ctx

It might well be all CSR overrides AND PostRender have executed in the meantime.

Then your success function deals with a global ctx that is not the same state as was passed to the lisPreRender function.

Making your Ajax call synchronous with async:false is a solution because your code doesn't continue until the success function has run

Alternative is to execute the Ajax call first and do all the CSR stuff in the success callback.

or

all this jQuery stuff returns a Promise by default, and are much easier to read

So you can ditch the succces and error callbacks and do

$.ajax({
  url: baseURL  + "/_api/Web/CurrentUser?$select=Id",
  method: "GET",                    
  headers: { "Accept": "application/json; odata=verbose" }        
}).then(function(data){
 //do your CSR stuff here
}).fail(function(e){
 //error
});

for more explanation and a no-jQuery solution see: REST call to get list not working