In what order does DOJO handle ajax requests?
Using the form in the dialog box, I am using Dojo in jsp to save the form to my database. Upon completion of this request, using dojo.xhrPost (), I send another request to update the dropdown in which the added form object that was just saved should be added, but for whatever reason, the request to update the dropdown is done before the form is saved in the database even if the form save is called first. Using Firebug, I can see that the getLocations () request completed before the sendForm () request. This is the code:
<button type="button" id="submitAddTeamButton" dojoType="dijit.form.Button">Add Team
<script type="dojo/method" event="onClick" args="evt">
sendForm("ajaxResult1", "addTeamForm");
dijit.byId("addTeamDialog").hide();
getLocations("locationsTeam");
</script>
function sendForm(target, form){
var targetNode = dojo.byId(target);
var xhrArgs = {
form: form,
url: "ajax",
handleAs: "text",
load: function(data) {
targetNode.innerHTML = data;
},
error: function(error) {
targetNode.innerHTML = "An unexpected error occurred: " + error;
}
}
//Call the asynchronous xhrGet
var deferred = dojo.xhrPost(xhrArgs);
}
function getLocations(id) {
var targetNode = dojo.byId(id);
var xhrArgs = {
url: "ajax",
handleAs: "text",
content: {
location: "yes"
},
load: function(data) {
targetNode.innerHTML = data;
},
error: function(error) {
targetNode.innerHTML = "An unexpected error occurred: " + error;
}
}
//Call the asynchronous xhrGet
var deferred = dojo.xhrGet(xhrArgs);
}
Why is this happening? Is there a way to make the first request complete before the second is completed?
To reduce the possibility of why this happens, I tried to set the value of the cache property in xhrGet to false, but the result is the same.
Please, help!
a source to share
As Alex said, asynchronous requests are something that their order is not guaranteed. If you want to guarantee their ordering, you can make them synchronous if you like. There is an option sync: true
I think you can send using request arguments. This causes the browser to hang until the request returns, so it is not recommended unless you have another option and the request is very fast.
You can also send any data you need along with the data of the current request. For example, suppose dropdown A determines the list selection available in dropdown B. Instead of submitting changes on dropdown A and then updating the options for dropdown B, what you can do is represent the value of A at the moment open dropdown B, and process it in server logic that determines selection B. (This assumes you have a dropdown widget with server-generated choices, not a standard tag.)
a source to share
The first A in Ajax means "asynchronous", which means that everything happens "at its own pace": most likely, requests are sent in the order you expect, but they end up the other way around, because the second is Faster. Yes, of course, you can wait for the second request to even start (send) before the first one completes - easiest, you can just put the beginning of the second request in the callback function of the first.
a source to share