본문 바로가기

js/jQuery

[jquery][ajax] promise 로 ajax result 리턴하기



function ViewData(param1) {

    GetAjaxeData(param1).then(function (data) {

        console.log("data : " + JSON.stringify(data));

    });

}



function GetAjaxeData(param1)

{

    var def = new $.Deferred();

    var formCollection = "param1=" + param1;



    $.post("ajax url", formCollection,

        function (data, textStatus, jqXHR) {

            if (data.Code == '0') {

                def.resolve(JSON.parse(data.Data));

            } else {

                def.reject();

            }

        });

    return def.promise();

}