I have a bunch of data inside a loop that I would like to send POST to the server through jQuery.
My code is similar to the following:
var patients = []
var post = function(theUrl, theData, callback){
$.ajax({
type: "POST",
url: theUrl,
data: theData,
success: callback,
contentType: "application/json"
});
}
var createdPatient = function(patient){
}
$('#saveAll').click(function(event) {
for (var i = 0;i < patients.length;i++) {
var json = JSON.stringify(patients[i]);
post("/openmrs/ws/rest/v1/patient", json, createdPatient);
}
});
When I run the code, only the last patient was saved on the server. How can I fix this erroneous result?
source
share