I have a “save” function in my JavaScript that is responsible for saving some information on my server. this function will be called in succession multiple times. I’m concerned about a race-condition forming. For this reason, I was hoping there was a way that I could pass a state variable from my calling function (save) to my request-response function (save_Succeeded). This way, I would be sure to know which item was successfully saved. Is there anyway to do this? Currently, I have the following, but I don’t know how to pass the state variable. Thanks!
function save(item) {
$.ajax({
url: getUrl(),
type: "POST",
data: JSON.stringify(item),
contentType: "application/json",
success: save_Succeeded
});
}
function save_Succeeded(result) {
alert("Display Saved Item ID here");
}
Of course, this is very important. Here is an example of one of my AJAX calls:
If you do
The
datavariable will contain the returned data from your server response. You can control your server response to have either success, or failure. You see here, I check to make sure the data is “Data saved successfully.”, if not i know there was an error, and I display that error message.In your case, you would have to alter your server code to return the id of the saved data if it was successful, then it would be contained in the
datavariable.