I try to rewrite information with $.ajax and show all on the display. It is about PUT and GET information from MongoDB. First I do PUT colunms of table with $.ajax in MongoDB. $.ajax with PUT is good working, because I see, that all is edited in MongoDB. After this action I want to get information from database. And here I have fail. I do not understand, why after PUT I get not actual information from data base.
$.ajax({
url: VASERVER_API_LOC + '/visualization/' + visid + '/',
contentType: 'application/json',
data: JSON.stringify(features),
type: 'PUT',
success: function (data) {
}
});
$.ajax({
url: VASERVER_API_LOC + '/visualization/' + visid + '/',
type: 'GET',
contentType: "application/json",
data: tmp_object,
success: function (tmp_object) {
var features = tmp_object.features;...
}
})
Ajax runs asynchronously (that's part of its name). What that means is that the two calls to $.ajax
may not complete in any order -- or ever complete.
If you have code that relies on the result of an asynchronous call, all work that relies on the call needs to be done in the callback for the ajax. The callback is fired after the ajax completes. You could use success
, or use deferreds (built in):
$.ajax(putRequest).done(function () {
$.ajax(getRequest);
});
AJAX is asynchronous, so that second AJAX call is running before your PUT
is completed, wrap that in a function, and call that from the success
of the first:
$.ajax({
url: VASERVER_API_LOC + '/visualization/' + visid + '/',
contentType: 'application/json',
data: JSON.stringify(features),
type: 'PUT',
success: function (data) {
getData();
}
});
function getData() {
$.ajax({
url: VASERVER_API_LOC + '/visualization/' + visid + '/',
type: 'GET',
contentType: "application/json",
data: tmp_object,
success: function (tmp_object) {
var features = tmp_object.features;...
}
})
}