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())
})
2
Answers
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
then in the function event call :
Here is an example of callback:
And now you can call it: