A method in JavaScript that sends two AJAX request questions simultaneously

Source: Internet
Author: User

There is a problem in doing the project today, presumably in a JSP page with a select drop-down search condition box and a Bootstrap table presentation list. Both are going to get the data in the background through Ajax, and it's done when the page loads.
The practice at that time was:

$(function() {    //Load drop-down menuSelectmenu (); /** Load Page table*/    varurl = ' Xxxx.do '; varCol =[{checkbox:true}, {field:' Operlogid ', Title:' xxx ', Formatter:function(value, row, index) {returnIndex + 1; }}, {field:' Empuserid ', Title:' xxx ', sortable:true}, {field:' Makedate ', Title:' XX ', sortable:true}, {field:' Menuname ', Title:' XX ', sortable:true}, {field:' Designation ', Title:' XX ', sortable:true    },    //{field: ' Content ', title: ' xx '},{field:' Result ', Title:' XX ', sortable:true    } ]; //Initializing the Load tableTableinit (URL, $ (' #tb ')), col);});/** * Load menu drop down*/functionSelectmenu () {$.ajax ({url:database+ "Xxx.do", type:' Post ', data: {}, Success:function(data) {//Alert (json.stringify (data));            varsel = ' <option value= ' > Please select </option> ';  for(varIinchdata) {sel+ = ' <option value= ' + data[i].menuname + ' > ' + data[i].menuname + ' </option> '; }            $("#sel_menuname"). HTML (SEL); }    });}

This completes the result is: in the Google browser page normal display, in Firefox browser will not periodically appear system exception error prompt!
the reasons for the final analysis are :
  From the principle of how asynchronous requests are executed, we know that when an asynchronous request is sent, the browser is not in a locked, waiting state, and the browser can do other things from an asynchronous request to the time it gets the result of the response. This means that multiple asynchronous requests are executed in parallel.
Two Ajax asynchronous requests (bootstrap tables are also AJX asynchronous requests at load time), because of the asynchronous problem, calling two Ajax async in the OnLoad method is actually equivalent to sending two requests at a time. Execution is fast and slow, depends on the size of the response data and the complexity of the background logic. Selectmenu () Requested drop-down list data is not shown, which means that Bootstrap's Tableinit method is faster than Selectmenu for the page, causing the page to go wrong.
Workaround :
There are, of course, many solutions to this problem, and there are three solutions available:
  (1) The execution of the AJAX2 () method is put to the last line of the success callback function of Ajax1 ().
(2) In the asynchronous request method of Ajax1 (), add a callback function: COMPLETE:AJAX2
(3) Set the asynchronous Ajax1 () to synchronous: Async:false
By these methods, the problem can be solved perfectly.

A method in JavaScript that sends two AJAX request questions simultaneously

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.