There is such a need in the project,Use ajax to load data and return to the page and assign values,Then the front end takes that value

This involves the order of the code,Sometimes the background has not returned data,But the following code has been executed,

So it will cause no value

$.ajax ({type:"post", url:"admin/pfmoptionruleitem.do", success:function (data) {$("#ruleitem"). val (data.ruleitem);//①}});return $("#ruleitem"). val ();//②

If ① the data has not been returned from the background at this time, ② the value cannot be obtained

The first letter of ajax is the beginning of asynchronous,This means that all operations are parallel,The order of completion does not matter.

The async parameter of $.ajax () is always set to true, which indicates that after the request starts,Other code can still execute.

If this option is set to false, this means that all requests are no longer asynchronous.This can also cause the browser to lock up.

Although this is not officially recommended,Just can't use too much,Otherwise it will cause poor user experience

Give a chestnut

alert ("setp 1");$.ajax ({url:"admin/pfmoptionruleitem.do", async:false, success:function (data) {alert ("hello ajax");//①}});alert ("setp 2");//②

When asyn is set to false, the ajax request is synchronized at this time,That is,At this time after the ajax block makes a request,

He will wait in this place,Will not go down to execute ② until ① is completed

At this time, the execution order is

setp 1

hello ajax

setp 2

If async is true, the execution order is

setp 1

setp 2

hello ajax

  • Previous The web page passes the value from the pop-up page radio box to the parent page
  • Next PHP tips for improving website efficiency