On Dec 4, 9:52 am, NMarcu <marcu.nico...@gmail.com> wrote: > Hello all, > > I have a strange problem with jquery load. I have this two loads > on a click event: > > action='first'; > $( '#operators_list_div' ).html( ' ' ).load( '{% url > add_save_view %}',{'action':action, }); > action='second'; > $( '#address_list' ).html( ' ' ).load( '{% url add_save_view %}', > {'action':action,}); > > On first load, I add something in db(I have more arguments send). At > the end I return a template that is a table and here works fine(I see > what I added. > In second load: Ex I add something with id = 1. I build a list(initial > the list is from 1 to 255). After the id=1 was added, the list will be > from 2 to 255. If i added an id=3, the list will be 1, 2, 4, 5,...255. > Here I want to return another template second.html that update a drop > down list with the list build. Here is the problem, sometime the list > is correct, other times not. After I add id 1, the list is correctly > updated(2,3...255), but sometimes is the default list(1,2...255). The > strange things is that is I put an alert, between the two load, it's > working fine > > action='first'; > $( '#operators_list_div' ).html( ' ' ).load( '{% url > add_save_view %}',{'action':action, }); > alert('brake'); > action='second'; > $( '#address_list' ).html( ' ' ).load( '{% url add_save_view %}', > {'action':action,}); > > Somebody know what I do wrong?
This isn't a Django query, but I'll answer it anyway. The thing to understand about Ajax queries is that they're *asynchronous* (that's what the first A stands for). That means the main Javascript flow doesn't wait for the query to return results before moving onto the next statement. If you have two Ajax queries following each other the way you have it, there's no guarantee that the first one will have finished by the time you call the second one, leading to unpredictable results. The way to fix this is to have the second call inside the callback function for the first one. This callback is executed when the first one completes. So: ( '#operators_list_div' ).html( ' ' ).load( '{% url add_save_view %}',{'action':action, }, function() { action='second'; $( '#address_list' ).html( ' ' ).load( '{% url add_save_view %}', {'action':action,}); } ); (By the way, this explains why adding an alert fixes it; the second Ajax call doesn't fire until you've clicked OK, by which time the first one has had time to complete.) -- DR. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.