0

I post data to my flask back-end and on a successful post I want an action to occur, there are functions for success and error but on a successful post its going to the error function.

            $.ajax({
                 url: '/some_route',
                 contentType: "application/json;charset=utf-8",
                 data: JSON.stringify(data),
                         dataType: "json",
                 type: 'POST',
                 success: function(response){
                    alert('Success!')
                 },
                 error: function(error){
                    alert('Failed...')
                 }
                 });

What is the ajax call expecting back to confirm it has been successful?

Heres my Flask route

@app.route('/some_route', methods=['POST'])
def some_route():
    if request.method == 'POST':
        # Do something

        return 'success', 200
Jesse Reza Khorasanee
  • 3,140
  • 4
  • 36
  • 53
Ari
  • 5,301
  • 8
  • 46
  • 120
  • 2
    Look at the network panel in your debugging tools and verify that the request is succeeding. – zero298 Jun 05 '19 at 22:58
  • 1
    It is succeeding, my data reaches flask and proceeds onwards, its just the response is correct and the ajax thinks the post failed. But I think Jesse below has the answer – Ari Jun 05 '19 at 23:50

1 Answers1

1

The flask response might not be triggering the ajax 'success'

This question recommends returning json:

return json.dumps({'success':True}), 200, {'ContentType':'application/json'} 
Jesse Reza Khorasanee
  • 3,140
  • 4
  • 36
  • 53