How to use ajax callbacks with jquery datatables i.e. call function on click?:
THIS WORKS
$(document).ready(function() {
var table = $('#example').DataTable();
$('#example tbody').on('click', 'tr', function () {
var data = table.row( this ).data();
alert( 'You clicked on '+data[0]+'\'s row' );
} );
} );
replacing alert( 'You clicked on '+data[0]+'\'s row' ); with Ajax call:
DOES NOT WORK
$(document).ready(function() {
var table = $('#example').DataTable();
$('#example tbody').on('click', 'tr', function () {
var data = table.row( this ).data();
//alert( 'You clicked on '+data[0]+'\'s row' );
$.ajax({
url: '/process',
data: data[0],
type: 'POST',
success: function(response) {
$("#response_placeholder").html(response);
},
error: function(error) {
console.log(error);
}
});
} );
} );
backend
#--app.py----
@app.route('/process', methods=['POST'])
def process_data():
data = request.form['data[0]'];
print data
return render_template('mypage.html', result=data)