Run some javascript code after a successfull execution of an ajax call

Run some javascript code after a successfull execution of an ajax call

garaggaraggaraggarag Posts: 3Questions: 1Answers: 0

Hi,

I've a datatable defined with the code (1) below

$('#forexHst').DataTable({
responsive: true,
pageLength: 30,
pagingType: 'simple',
displayStart: 0,
dom: 'Bfrtip',
searching: true,
processing: true,
ajax:
{
url: "myurl",
type: "POST",
data: { currency: 'USD' }
},
columns:
[
{ data: 'Date' },
{ data: 'Value' }
],
order: []
});

Is it possible to execute some javascript code after the successfully execution of the ajax call?
I'm able to execute some javascript if I run a code (2) like the following

$.ajax({
type: "POST",
url: "myurl",
data: JSON.stringify(
{
currency: ?USD'
}
),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
if (response.d != null && response.d != '' && $.parseJSON(response.d).Id > 0) {
//do something
}
else
//print error
},
error: function (response) {
//print error
}
});

but it is not clear to me how to define a success function in the code (1) used to setup the datatable.

Thanks,
Antonio

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Hi Antonio,

    Is it possible to execute some javascript code after the successfully execution of the ajax call?

    Yes - use the xhr event rather than specifying a "success" function for the ajax object (that won't work, since it would overwrite DataTables' use of success.

    Allan

Sign In or Register to comment.