ajax 를 여러 번 요청 하고 결과 집합 을 합 칩 니 다 (ajax 비 비동기)

코드 를 바로... 안에 주석 이 있어 요.


    
        
        
        <script src="js/jquery-3.4.1.js"/>
    
    
        <script><span style="color:#000000;">
            
            (</span><span style="color:#0000ff;">function</span><span style="color:#000000;">() {
                </span><span style="color:#0000ff;">var</span> data = {stuId:'4231'<span style="color:#000000;">};
                </span><span style="color:#0000ff;">var</span> list =<span style="color:#000000;"> [];
                getInfo(</span><span style="color:#0000ff;">function</span><span style="color:#000000;">(data,obj){
                    </span><span style="color:#008000;">//</span><span style="color:#008000;">                 3   ,      </span>
                    <span style="color:#0000ff;">if</span><span style="color:#000000;">( data ){
                        list.push(data);
                    }</span><span style="color:#0000ff;">else</span><span style="color:#000000;">{
                        </span><span style="color:#008000;">//</span><span style="color:#008000;">     </span>
                        <span style="color:#0000ff;">var</span> stuIds = obj.stuId.split(""<span style="color:#000000;">);
                        </span><span style="color:#008000;">//</span><span style="color:#008000;">         </span>
                        <span style="color:#0000ff;">for</span> (<span style="color:#0000ff;">var</span> i = 0; i < stuIds.length; i++<span style="color:#000000;">) {
                            </span><span style="color:#008000;">//</span><span style="color:#008000;">     </span>
                            <span style="color:#0000ff;">var</span> obj =<span style="color:#000000;"> {stuId: stuIds[i]};
                            </span><span style="color:#008000;">//</span><span style="color:#008000;">    list             </span>
                            <span style="color:#0000ff;">if</span>( list.length >= 2<span style="color:#000000;"> ){
                                </span><span style="color:#008000;">//</span><span style="color:#008000;">     </span>
                                <span style="color:#0000ff;">break</span><span style="color:#000000;">;
                                </span><span style="color:#008000;">//</span><span style="color:#008000;">throw Error("    ,      ...");</span>
                            }<span style="color:#0000ff;">else</span><span style="color:#000000;">{
                                getInfo(</span><span style="color:#0000ff;">function</span><span style="color:#000000;">(data){
                                    </span><span style="color:#0000ff;">if</span><span style="color:#000000;">( data ){
                                        list.push(data);
                                    }
                                },obj);
                            }
                        }
                    }
                },data);
                
                console.log(list);
            })();
            
            </span><span style="color:#008000;">//</span><span style="color:#008000;">     </span>
            <span style="color:#0000ff;">function</span><span style="color:#000000;"> getInfo(callback,stuId) {
                $.ajax({
                    async:</span><span style="color:#0000ff;">false</span>,<span style="color:#008000;">//</span><span style="color:#008000;">     </span>
                    type: 'get', <span style="color:#008000;">//</span><span style="color:#008000;">    </span>
                    url: 'http://127.0.0.1:8080/stu/findById', <span style="color:#008000;">//</span><span style="color:#008000;">       </span>
                    contentType: "application/json", <span style="color:#008000;">//</span><span style="color:#008000;">             </span>
                    data: stuId, <span style="color:#008000;">//</span><span style="color:#008000;">    </span>
                    success: <span style="color:#0000ff;">function</span>(data) { <span style="color:#008000;">//</span><span style="color:#008000;">      </span>
<span style="color:#000000;">                        callback(data,stuId);
                    },
                    error: </span><span style="color:#0000ff;">function</span>(error) { <span style="color:#008000;">//</span><span style="color:#008000;">        </span>
                        alert("    。"<span style="color:#000000;">);
                    }
                });
            }
        </span></script>
    
</code></pre> 
   </div> 
   <p> </p> 
  </div> 
  <p>   :https://www.cnblogs.com/oukele/p/11576439.html</p> 
 </div> 
</div>
                            </div>
                        </div>

좋은 웹페이지 즐겨찾기