wait for a jquery ajax callback from calling function

后端 未结 4 1650
悲哀的现实
悲哀的现实 2020-11-27 07:01

I have reviewed a lot of answers to this type of question and now I am confused as to the best way. Given the latest jquery, I am wanting to

  1. Call an ajax funct
4条回答
  •  悲哀的现实
    2020-11-27 07:31

    As previously mentioned, using Callbacks.

    function process(url, params, successCallback, errorCallback) {
        $.ajax({
            success : successCallback,
            error : errorCallback,
            data : params,
            url : url,
            type : 'POST',
            dataType : 'json'
        });
    }
    
    process(
        'http://www.google.co.uk', 
        { 
            param1 : 'a' 
        }, 
        function(resp) { 
            alert('Success');
        },
        function() {
            alert('Uh oh');
        }
    );
    

    You can then pass any function to process and it will be called on success/error.

提交回复
热议问题