How send data row from data table js to flask and delete it
How send data row from data table js to flask and delete it
kepsnius
Posts: 14Questions: 6Answers: 0
i have issue to send data in flask from js. I try use ajax. I have data table in website, need make delete function then i press button delete that row.
js code
$('#calEvents').on( 'click', 'button', function () {
var data = table.row( $(this).parents('tr') ).data();
var row= this.parentNode.parentNode;
alert(row.rowIndex);
var confirmalert = confirm("Are you sure?");
if (confirmalert == true) {
document.getElementById("calEvents").deleteRow(row.rowIndex);
new_item = $('#data').val() //value I want to send
$.ajax({
url: '/delete_value',
type: 'POST',
data: new_item,
success: function(response){
$('#main').text(response)
}
})
}else{
alert('Invalid ID.');
}
});
Python code
@api.route('/delete_value', methods=['POST'])
@login_required
def get_javascript_data(new_item):
new_item = request.get_data()
query = "DELETE FROM CalEvents WHERE eventId = 1"
app.logger.debug(query)
cursor.execute(query)
return redirect(url_for('calendar/get'))
if u got some tips or tricks please tell me.
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
What happens, do you get the value sent to Flask with this code?
You haven't provided enough info to understand the problem. Please describe where the problem occurs and the troubleshooting steps you have taken.
Kevin
my mistake code has to be new_item = $('#input').data(), t
then i allert (just for check for data) i get
so its all good i think.
Then i debug it i got
error 500 (INTERNAL SERVER ERROR)
and in /delete_value i get
TypeError: get_javascript_data() missing 1 required positional argument: 'new_item'
and function not working.
i get value, but in flask cant accept it or something like what
Sounds like you shouldn't define the
new_item
parameter here:Kevin
then i get this get_javascript_data() missing 1 required positional argument: 'new_item'
Now i try do this.
And python
And i get data in jaison and cant pick up value , idk how to fix it. Get error.
pymssql.ProgrammingError: (102, b"Incorrect syntax near '0'.DB-Lib error message 20018, severity 15:\nGeneral SQL Server error: Check messages from the SQL Server\n")
for example if u make querty
query = "DELETE FROM CalEvents WHERE eventId =1"
then delete from database
Your questions are specific to how to process the request in Flask. There are lots of ways to setup routes in Flask and this forum is not a place to learn how. A better place for Flask questions is to look at Stack Overflow threads like this one or review the available Flask tutorials like this.
If you have Datatables specific questions please post them.
Kevin
This is specific to Python. You need to learn how to handle strings with variables. The easiest way is to concatenate them like this:
query = "DELETE FROM CalEvents WHERE eventId = " + data[0] + '"'
Again for Python questions please use Stack Overflow.
Kevin