jQuery的多个ajax请求

jQuery的多个ajax请求,jquery,ajax,Jquery,Ajax,我在下面的代码中发现了问题: function fillStateList(cid) { $.ajax( { type: "POST", contentType: "application/json; charset=utf-8", url: someurl, data: "{'countyId': '" + cid + "'}", dataType:

我在下面的代码中发现了问题:

function fillStateList(cid) {
     $.ajax(
        {
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: someurl,
            data: "{'countyId': '" + cid + "'}",
            dataType: "json",
            success: function (data) {
                if (data.d.length > 0) {
                    //alert('r- ' + stateId); // <-- if uncomment this line, the code works fine !    
                    // code to bind select list
                                       }
                                     }
         });
}
函数fillStateList(cid){
$.ajax(
{
类型:“POST”,
contentType:“应用程序/json;字符集=utf-8”,
url:someurl,
数据:“{'countyId':'”+cid+“}”,
数据类型:“json”,
成功:功能(数据){
如果(数据长度>0){

//警报('r-'+stateId);//如果从两个不同的来源(例如服务器的响应)异步调用,则有可能
警报
会导致某些故障

警报也不是调试数据的最佳方式。 改用
console.log
,这是一种更好的调试数据的方法。你可以传递一个对象或数组,开发者控制台(如chrome或firefox的firebug?)会很好地显示它

要链接ajax请求,您可能需要使用

这里我使用
$。当
(请参阅)链接延迟时

您还可以使用
done
回调中的参数,其中每个参数都是延迟传递到when的参数

$.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) ).done(function( a1, a2 ) {
   var data = a1[ 0 ] + a2[ 0 ]; // a1[ 0 ] = "Whip", a2[ 0 ] = " It"
   if ( /Whip It/.test( data ) ) {
       alert( "We got what we came for!" );
   }
});
这里
a1
是对第一个ajax请求的引用,
a2
是对第二个ajax请求的引用。
在这里,您还可以看到是否有一个问题没有解决…

您有一个条件,在执行下一个AJAX请求之前,您的AJAX请求必须完成

要使其工作,请执行以下两个选项之一:

  • success
    函数进行后续ajax调用,以确保在上一次调用完成时进行下一次调用

  • 使AJAX调用同步:

  • $.ajax({
    async:false,
    ...
    }
    
    解决了这个问题

    // global variable to check whether the fillStateList already running (waiting for response) or not             
    var IsBindingState = false;
    
    // An extension method to fillStateList function to synchronize the requests (not to call fillStateList directly, use this method)
    function fillStateListSync(cid) {
                        if (!IsBindingState) {
                            fillStateList(cid);
                        }
                        else {
                            var loop = setInterval(function () {
                                if (!IsBindingState) {
                                    fillStateList(cid);
                                    clearTimeout(loop);
                                }
                            }, 100);
                        }
                    }
    
    // function to bind state list
    function fillStateList(cid) {
         IsBindingState = true;
         $.ajax(
            {
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: someurl,
                data: "{'countyId': '" + cid + "'}",
                dataType: "json",
                success: function (data) {
                    if (data.d.length > 0) {
                        //alert('r- ' + stateId); // <-- if uncomment this line, the code works fine !    
                        // code to bind select list
                                    }
                             IsBindingState = false;
                          },
                error: function (result) {
                             IsBindingState = false;
                            }
             });
    }
    
    //用于检查fillStateList是否已在运行(等待响应)的全局变量
    var IsBindingState=false;
    //fillStateList函数的扩展方法,用于同步请求(不要直接调用fillStateList,请使用此方法)
    函数fillStateListSync(cid){
    如果(!IsBindingState){
    填充状态列表(cid);
    }
    否则{
    变量循环=设置间隔(函数(){
    如果(!IsBindingState){
    填充状态列表(cid);
    清除超时(循环);
    }
    }, 100);
    }
    }
    //函数绑定状态列表
    函数fillStateList(cid){
    IsBindingState=true;
    $.ajax(
    {
    类型:“POST”,
    contentType:“应用程序/json;字符集=utf-8”,
    url:someurl,
    数据:“{'countyId':'”+cid+“}”,
    数据类型:“json”,
    成功:功能(数据){
    如果(数据长度>0){
    
    //警报('r-'+状态ID);//听起来你有一个竞争条件。AJAX请求完成的顺序重要吗?如果是,你应该一个接一个地链接它们。如果你将alert改为console.log,它是否仍然有效,并且日志条目是否与预期的一样多?@Rory McCrossan:如何链接它们?发出第一个AJAX请求,然后递归发出下一个请求在回调过程中使用默认值。从
    success
    处理程序发出后续AJAX请求。
    async:false
    那么为什么使用AJAX?@AshokDamani应该可以工作,用您尝试过的内容更新您的问题,以便我们更好地了解问题。
    // global variable to check whether the fillStateList already running (waiting for response) or not             
    var IsBindingState = false;
    
    // An extension method to fillStateList function to synchronize the requests (not to call fillStateList directly, use this method)
    function fillStateListSync(cid) {
                        if (!IsBindingState) {
                            fillStateList(cid);
                        }
                        else {
                            var loop = setInterval(function () {
                                if (!IsBindingState) {
                                    fillStateList(cid);
                                    clearTimeout(loop);
                                }
                            }, 100);
                        }
                    }
    
    // function to bind state list
    function fillStateList(cid) {
         IsBindingState = true;
         $.ajax(
            {
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: someurl,
                data: "{'countyId': '" + cid + "'}",
                dataType: "json",
                success: function (data) {
                    if (data.d.length > 0) {
                        //alert('r- ' + stateId); // <-- if uncomment this line, the code works fine !    
                        // code to bind select list
                                    }
                             IsBindingState = false;
                          },
                error: function (result) {
                             IsBindingState = false;
                            }
             });
    }