0

I am making a function that posts AJAX requests.

I need to call it from multiple pages, how can I make a callback method inside the done method? Or even a second callback for beforesend events.

function post_ajax(URL,CONTENT_TYPE,$RENDERING_DIV,PARAMS={},callback_function){
   $.post(URL, PARAMS, null, CONTENT_TYPE)
      .done(data => {
          callback_function()
      }).fail(() => alert('an error occurred'));
}

//function call
$('#mybtn').click(()=>{
    post_ajax('a_script.php','html','#div_res',{},callback_function())
})
8
  • You do return $.ajax then let your calling function handle the promise. Commented May 14, 2020 at 14:09
  • Depending on your requirements, as presented, you'd be better off using global ajax event handlers: api.jquery.com/category/ajax/global-ajax-event-handlers Commented May 14, 2020 at 14:12
  • A global ajax event handler will not be dynamic. I need to make a dynamic function based ajax post request that i can be able to manipulate what ever is inside the done promise within the function call Commented May 14, 2020 at 14:30
  • @freedomn-m please write an answer to explain better and why the provided answers are not usable. Commented May 14, 2020 at 14:35
  • The key here, which you should have included in your question, is that you want to manipulate the data within .done before passing it to the callback function. Providing the use-case / reason helps provide a sensible answer otherwise it looks like an XY Problem (re-inventing an existing wheel) Commented May 14, 2020 at 14:36

2 Answers 2

1

Here is an example of callback:

function post_ajax(URL, CONTENT_TYPE, $RENDERING_DIV, PARAMS={}, callback_function) {
    $.post(URL, PARAMS, null, CONTENT_TYPE)
    .done(data => {
        if (typeof callback_function === 'function') {
            callback_function(data);
        }
    })
    .fail(() => alert('an error occurred'));
}

And now you can call it:

$('#mybtn').click(() => {
    post_ajax('a_script.php', 'html', '#div_res',{}, (data) => { 
        console.log(data);
        alert('You are here'); 
    });

})
Sign up to request clarification or add additional context in comments.

Comments

1

your declaration is correct (expet sending result data to callback), the problem is in the call manner of the callback function

you need to pass a function declaration (not execution) or, just implement that function directly , by example :

first you have to pass the result as parameter to your callback in the done

.done(data => {
        callback_function(data) // <---- here pass data
    }).fail(() => alert('an error occurred')

then in the function event call :

//function call
$('#mybtn').click(()=>{
    post_ajax('a_script.php','html','#div_res',{},(data) => {
        console.log(data);
        //some stuff here manipulating data 
    });
})

4 Comments

what if i want to make a callback that accepts no parameters? Can you show an example?
In javascript if your function doesn't have parameters, then calling it with parameters will have those parameters ignored
Better to pass along as-is: $.post(..).done(callback_function) then it will always get the correct parameters that done gets - but better still not to use a callback_function and use the promise.
yes ass @freedomn-m mentiened , to pass without parameter (mean catch every param sent) just se .done(callback_function);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.