Flask, how to return a success status code for ajax call Flask, how to return a success status code for ajax call flask flask

Flask, how to return a success status code for ajax call


About Responses in Flask:

About Responses

The return value from a view function is automatically converted into a response object for you. If the return value is a string it's converted into a response object with the string as response body, a 200 OK status code and a text/html mimetype. The logic that Flask applies to converting return values into response objects is as follows:

  1. If a response object of the correct type is returned it's directly returned from the view.
  2. If it's a string, a response object is created with that data and the default parameters.
  3. If a tuple is returned the items in the tuple can provide extra information. Such tuples have to be in the form (response, status, headers) or (response, headers) where at least one item has to be in the tuple. The status value will override the status code and headers can be a list or dictionary of additional header values.
  4. If none of that works, Flask will assume the return value is a valid WSGI application and convert that into a response object.

So, if you return text string (as you are doing), the status code that your AJAX call has to receive is 200 OK, and your success callback must be executing. However, I recommend you to return a JSON formatted response like:

return json.dumps({'success':True}), 200, {'ContentType':'application/json'} 


As an option to aabilio's answer, you can use the jsonify method in Flask which automatically sets the Content-Type:

from flask import jsonifyresp = jsonify(success=True)return resp

You can (optionally) set the response code explicitly:

resp.status_code = 200


In addition to the answers already posted, I find using the make_response method in Flask (from version 0.6) to be a clearer alternative especially when you need to return status codes with the response JSON for APIs from Flask:

from flask import jsonify, make_response# ... other code ...data = {'message': 'Created', 'code': 'SUCCESS'}return make_response(jsonify(data), 201)

Also, this approach allows you to pass a response code (201) and will automatically set the Content-Type header to application/json.