jquer ajax的方法返回值

需求:定义一个方法,返回FALSE or TRUE。

  初始化temp = false,如果json.data.cusLogs[0] != null,就赋值为true,然后返回。

  然而实际执行过程却是return temp 先于success:function(){}执行。就导致了返回值不符合我们的期望。因为jquery ajax默认是异步的。所以只需在cache:false后加上asyn:false就好了。

function haveLogs(orderId){
    var temp = false;
    layer.load(2,{shade:0.3});
    $.ajax({
        url: "/man/pc/cus/log?orderId=" + orderId ,
        dataType: "json",
        type: "GET",
        cache: false,
        headers: {
            'X-HTTP-Method-Override': "GET",
            'Accept': 'application/json',
            'Content-Type': 'application/json',
        },
        success: function (json) {
            layer.closeAll('loading');
            if (json.data.status == -1 || json.data.status == "-1") {
                layer.msg(json.data.message);
                return;
            }
            if (json.data.cusLogs[0] != null){
                temp = true;
            }
        },
        error: function () {
            layer.closeAll('loading');
        }
    });
    return temp;
}

  

posted on 2017-03-28 19:33  wantcrazy  阅读(693)  评论(0编辑  收藏  举报

导航