Is there any way to wait for AJAX response and halt execution?

前端 未结 6 981
囚心锁ツ
囚心锁ツ 2020-12-24 10:42

Here is some code I\'d like to execute. I\'d like to wait for AJAX response so I can return something from the server. Any way to achieve this?

function func         


        
6条回答
  •  佛祖请我去吃肉
    2020-12-24 11:17

    Method 1:

    function functABC(){
        $.ajax({
            url: 'myPage.php',
            data: {id: id},
            success: function(data) {
                return data;
            },
            complete: function(){
                  // do the job here
             }
        });
    }
    
    var response = functABC();
    

    Method 2

    function functABC(){
        $.ajax({
            url: 'myPage.php',
            data: {id: id},
            async: false,
            success: function(data) {
                return data;
            }        
        });
    
       // do the job here
    }
    

提交回复
热议问题