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

kepsniuskepsnius Posts: 14Questions: 6Answers: 0
edited July 2020 in Free community support

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

Answers

  • kthorngrenkthorngren Posts: 21,309Questions: 26Answers: 4,947

    What happens, do you get the value sent to Flask with this code?

         new_item = $('#data').val() //value I want to send
         $.ajax({
             url: '/delete_value',
             type: 'POST',
             data: new_item,
    ....
    

    You haven't provided enough info to understand the problem. Please describe where the problem occurs and the troubleshooting steps you have taken.

    Kevin

  • kepsniuskepsnius Posts: 14Questions: 6Answers: 0
    edited July 2020

    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.

  • kepsniuskepsnius Posts: 14Questions: 6Answers: 0


    i get value, but in flask cant accept it or something like what

  • kthorngrenkthorngren Posts: 21,309Questions: 26Answers: 4,947

    Sounds like you shouldn't define the new_item parameter here:

    def get_javascript_data(new_item):

    Kevin

  • kepsniuskepsnius Posts: 14Questions: 6Answers: 0

    then i get this get_javascript_data() missing 1 required positional argument: 'new_item'

  • kepsniuskepsnius Posts: 14Questions: 6Answers: 0

    Now i try do this.

     $.ajax({
            type: "GET",
            contentType: "application/json;charset=utf-8",
            url: "api/v1/delete_value",
            traditional: "true",
            data: JSON.stringify({data}),
            dataType: "json"
            }); 
    

    And python

    @api.route('/delete_value')
    @login_required
    def get_javascript_data():
        data = request.get_data()
        
       query = "DELETE FROM CalEvents WHERE eventId = data[0]"
       app.logger.debug(query)
       cursor.execute(query)
        return "OK"
    

    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

  • kthorngrenkthorngren Posts: 21,309Questions: 26Answers: 4,947

    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

  • kthorngrenkthorngren Posts: 21,309Questions: 26Answers: 4,947
    edited July 2020 Answer ✓

    query = "DELETE FROM CalEvents WHERE eventId = data[0]"

    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

This discussion has been closed.