如何用条件执行ajax成功函数

问题描述 投票:0回答:2

我试图在特定条件下进入ajax成功回调。我正在向ajax函数传递一个参数,并且基于参数匹配我正在尝试执行该函数。

我有一个if条件,如果参数是abc然后url将是一些东西,如果参数是xyz然后我试图跳转到成功函数。但是我无法做到。我怎么能这样做,这就是我试图做的事情。

function Call(type){
    $.ajax({
            if(type != 'xyz'){
                var url = "/param1/api/me?id="+kId+"&clickType="+type;
            }else{
                //jump to success callback
            }           
            success = function(data){
                if(type == "abc"){
                    console.log('abc');
                } else if(type == "asd"){
                    console.log('asd');
                } else if(type == "gst"){
                    console.log('gst');
                } else if(type == "xyz"){
                    console.log('xyz');
                };
            });
    });
}
javascript jquery
2个回答
0
投票

哦。你的意思是这样的?

function Call(type){
    if(type != 'xyz'){
        $.ajax({
            url: "/param1/api/me?id="+kId+"&clickType="+type,
            success: function (res) {
                console.log(res);
            }
        });
    }elseif(type == "abc"){
        console.log('abc');
    } else if(type == "asd"){
        console.log('asd');
    } else if(type == "gst"){
        console.log('gst');
    } else if(type == "xyz"){
        console.log('xyz');
    }
}

0
投票

试试这段代码。

$.ajax({
        type: "POST",
        url: ajaxurl,
        data: {},
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            if (data.d == "-505") {
               // Error page
            }
            else if (data.d == "-501") {
              // other page
            }
            successfunction(data);
        },
        error: function (data) {
            //$.messager.alert('Warning', errormessage);

        }
    });
© www.soinside.com 2019 - 2024. All rights reserved.