IT/development

[JavaScript] Ajax ๊ฒฐ๊ณผ๊ฐ’ ๋ณ€์ˆ˜ ์ €์žฅ

์•Œ ์ˆ˜ ์—†๋Š” ์‚ฌ์šฉ์ž 2022. 11. 22.

๋ชฉ์ฐจ

    ๋ฏธ๋ž˜์˜ ๋‚˜๋ฅผ ์œ„ํ•ด ๊ธฐ๋ก์„ ๋‚จ๊น€ ๐Ÿ˜„

    ajax๋ฅผ ํ†ตํ•ด ์–ป์–ด์˜จ ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•˜๋ ค๋ฉด ajax์‹œ ์˜ต์…˜์— async: false๋ฅผ ์ค˜์„œ ๋™๊ธฐ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋„๋ก ๋ณ€๊ฒฝํ•ด์•ผ ํ•œ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ true

    ์•„๋‹ˆ๋ฉด undefined๋กœ return ๋œ๋‹ค.

    function duplicateCheck(menuCode) {
        let duplicate_result;
        $.ajax({
            url : '/admin/menu/duplicateCheck',
            async: false,
            type : 'post',
            data : {'menuCode': menuCode},
            success: function (data) {
                // alert("data : " + data);
                duplicate_result = data;
            }
        });
        return duplicate_result;
    }

     

    Reference : https://stackoverflow.com/questions/8077443/save-response-from-jquery-ajax-function

     

    Save response from jQuery ajax function

    I've got a custom function, which calls ajax request and I'm able to save response data to variable and return it. 0 is always returned, but alert shows e.g. 3712. Here is the function: function ...

    stackoverflow.com

    ๋Œ“๊ธ€