在这种情况下,如何管理Ajax调用执行

时间:2014-12-04 16:21:10

标签: jquery ajax

我有一个页面,其中cust_id从一个页面传递到另一个页面(来回)

我面临的问题是有时候cust_id可能是未定义的或空的

在这种情况下,我想基于Ajax调用来构建customer_id 这是我的代码。

var cust_id = getParameterByName('cust_id');

$(document).on('click', '#saveaddress', function() {
 cust_id = ''
        if(cust_id==''||cust_id=='undefined'||cust_id=='')
        {

        }
    if(somecondition)
    {
        $.ajax({
            type: 'GET',
            url: url+'/OMS/oms1/chdsavenewaddresslabel?mobile_number='+phonenumber+'&location='+location+'&cust_id='+cust_id+'&address1='+address1+'&address2='+address2+'&landmark='+landmark+'&locality='+locality+'&area='+area+'&city='+city+'&state='+state,
            jsonpCallback: 'jsonCallback',
            dataType: 'jsonp',
            jsonp: false,
            success: function(response) {



            },
            error: function(e) {
                alert('Error inside Fill form request');
            }
        });
    }
    else
    {
        alert('Please correct the Error');
    }

});



function fetchCustomerId(uuid)
{
  $.ajax({
        type: 'GET',
       url: url+'/OMS/oms1/fetchcustomerId?uuid='+uuid,
        jsonpCallback: 'jsonCallback',
        cache: true,
        dataType: 'jsonp',
        jsonp: false,
        success: function (responsesss) {
            var res = responsesss;
            var cust_id = res.Id;
       },   
        error: function (e) {
        }
    });
 return cust_id;
}

我面临的问题是两个Ajax调用混淆了

请让我知道如何独立执行每个Ajax调用

1 个答案:

答案 0 :(得分:0)

查看jquery的承诺文档:http://api.jquery.com/jquery.when/

有一个将两个ajax调用链接在一起的示例。您可能需要稍微重写一下您的功能,但它会让您对需要做的事情有所了解。

$.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) )
  .then( myFunc, myFailure );
相关问题