Home > front end >  At five o 'clock in the morning: $. Ajax three layers of nested loop, an asynchronous request o
At five o 'clock in the morning: $. Ajax three layers of nested loop, an asynchronous request o

Time:09-30

The business scenario,
Three layers of nested loop
Specify a province, access to all the cities in the province first, to get each counties under the city, finally obtain each county schools, under the data actually get the province's school
Target requirements: according to finish first obtain a city, to retrieve the next city, when the city is not complete, don't start the next city,
But according to the following code, and finally can get entirely, but found a city without reading, began the next city, try the use done (), then () are not solved, where is the problem?
O great god glad

 

<style>
# # btnGet btnGetAll {
Width: 150 px;
}
</style>
<script SRC="https://bbs.csdn.net/topics/js/jquery.2.1.4.min.js" & gt; </script>

<body>













//to obtain a list of all the city
The function of the Get ()
{
Var procode=$(" # prov "). Val ();.
Var proname=$(" # prov option: selected "). The text ();
$. Ajax ({
Type: "GET",
Url: "ajax/index. Aspx? M=list& Procode procode="+ +" & amp; Proname="+ proname,
XML dataType: ", "
Success: the function (cityxml)
{
GetCountry (cityxml, 0);
}
});
}

//get counties list
The function GetCountry (cityxml, index)
{
Var ctCnt=$(cityxml). Find (" T "). The length;
Var code=$(cityxml). Find (" T "). Eq. (index), find (" code "). The text ();
Var name=$(cityxml). Find (" T "). Eq. (index), find (" name "). The text ();
If (index & gt;=ctCnt) return;
$(" # ict "). The text (" total "+ ctCnt +" a city, is currently the first "+ (index + 1) +" : "+ name);
$. Ajax ({
Type: "GET",
Url: "ajax/index. Aspx? M=listcountry& Code="+ code,
XML dataType: ", "
Success: the function (countryxml)
{
//get the current county school
GetSchool (countryxml, 0);
}
}). Then (function ()
{
Index++;
GetCountry (cityxml, index);
});
}

//get the school list
The function GetSchool (countryxml, index)
{
Var ctCnt=$(countryxml). Find (" T "). The length;
Var code=$(countryxml). Find (" T "). Eq. (index), find (" code "). The text ();
Var name=$(countryxml). Find (" T "). Eq. (index), find (" name "). The text ();
If (index & gt;=ctCnt) return;
$(" # icn "). The text (" total "+ ctCnt +" counties, is currently the first "+ (index + 1) +" : "+ name);
$. Ajax ({
Type: "GET",
Url: "ajax/index. Aspx? M=listschool& Code="+ code,
XML dataType: ", "
Success: the function (XML)
{
//get the current county school
$(" # ics "). The text (" the current area, a total of "+ $(XML). The text () +" school!" );
}
}). Then (function ()
{
Index++;
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • Ajax
  • Related