Don't forget the 'A' in Ajax stands for Asynchronous. When you make an Ajax call you're saying "go out and get the following from the server, and when you come back successfully with a result, call this function passing the result in". This means you make an Ajax call and it fires off that request to the server and doesn't wait before executing the next line of JavaScript. When the server comes back, then your callback is triggered.
Note that you do have the option to make a synchronous or blocking call, but as the documentation notes, this can make the browser unresponsive: http://docs.jquery.com/Ajax/jQuery.ajax#toptions async Boolean Default: true By default, all requests are sent asynchronous (i.e. this is set to true by default). If you need synchronous requests, set this option to false. *Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active.* - Richard On Tue, Nov 10, 2009 at 12:22 PM, Ravi <[email protected]> wrote: > Hi: I have a ajax calling within a function that is called when user > clicks the submit button. For some reason the action corresponding to > submit button completes before the ajax call inside. Below is the > code. When I click the Submit button on the page. The first alert > shows "CustList is " (an empty cust list). Then it starts item inside > the each function (Current Shortname is a1, Current Shortname a2 etc). > Any reason why the execution doesn't go in the order I have coded? > > $('#newRSubmit').bind('click', function() { > . > . > . > custList = ''; > $.ajax({ > url: 'u_GetAllowedCustListXML.cgi', > data: 'userid=' + nco_userid + '&useremail=' + > nco_emailid, > type: 'GET', > dataType: 'xml', > timeout: 30000, > error: function(){ > jAlert('Error: Unable to get data from CMDB', 'Error'); > }, > success: function(xml){ > $(xml).find('customer').each(function(){ > var shortName = $(this).attr("shrtname"); > alert ("Current Shortname: " + shortName); > if(custList == '') custList = shortName; > else custList += ',' + shortName; > }); > } > }); > alert("CustList is " + custList); > }); > > -- > > You received this message because you are subscribed to the Google Groups > "jQuery UI" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]<jquery-ui%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/jquery-ui?hl=. > > > -- You received this message because you are subscribed to the Google Groups "jQuery UI" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/jquery-ui?hl=.
