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

JqueryPythonAjaxFlask

Jquery Problem Overview


On the server-side, I am just printing out the json-as-dictionary to the console

@app.route('/',methods=['GET','POST'])
@login_required
def index():
    if request.method == "POST":
        print request.json.keys()
    return "hello world"

Now, whenever I make a post request via ajax, the console prints out the dictionary with the contents I need.

On the client-side, I have been trying to use various methods to execute some jquery based on a successfull ajax call. I just realized that this might be an error on my server-side, i.e I am not sending any request header to tell jquery that its ajax call was a success.

So how do I send an OK status back to my client to tell it everything is all right?

For the sake of completeness, here is my clientside code

$.ajax({
    type: 'POST',
    contentType: 'application/json',
    data: JSON.stringify(myData),
    dataType: 'json',
    url: '/',
    success: function () {
        console.log("This is never getting printed!!")
    }});

Jquery Solutions


Solution 1 - Jquery

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.

  1. 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.
  2. 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'} 

Solution 2 - Jquery

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

from flask import jsonify

resp = jsonify(success=True)
return resp

You can (optionally) set the response code explicitly:

resp.status_code = 200

Solution 3 - Jquery

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 Flask, jsonify, make_response

app = Flask(__name__)


@app.route('/do_thing')
def do_thing():
    # ... other code ...
    data = {'message': 'Done', 'code': 'SUCCESS'}
    return make_response(jsonify(data), 201)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080)

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

Solution 4 - Jquery

When returning a response using jsonify just add the status_code as the second parameter of the return. I've used jsonify in this admin_required decorator with the 401 unauthorized HTTP code. Example:

return jsonify({'error': 'Admin access is required'}), 401

Full example:

def admin_required(f):
    @wraps(f)
    def decorated(*args, **kwargs):
        if current_user and hasattr(current_user, 'user_type') and current_user.user_type == 'admin':
            return f(*args, **kwargs)
        else:
            if '/api/' in request.url_rule.rule:
                return jsonify({'error': 'Admin access is required'}), 401

            flash(_('Admin access required'))
            return redirect(url_for('main.public_index'))

    return decorated

Solution 5 - Jquery

You can also use the response_class together with the json dump. Check the example below:

from flask import Flask
from flask import json

app = Flask(__name__)

@app.route(/info)
def getInfo():
    response = app.response_class(
            response=json.dumps({"status":"success","code":0,"data":{"Name":"Eyong","Age":30}}),
            status=200,
            mimetype='application/json'
    )
    return response

Solution 6 - Jquery

In newer versions of flask, it'll handle conversions in the response for you now. Your response can be simplified to returning your dictionary and response code like this:

@app.route("/my_route")
def my_route():
    data = {"status": "success", "some key", "some value"}
    return data, 200

data doesn't necessarily need to be a dictionary either, you could also pass in a string too!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionyayuView Question on Stackoverflow
Solution 1 - JqueryaabilioView Answer on Stackoverflow
Solution 2 - JqueryPhilip BergströmView Answer on Stackoverflow
Solution 3 - JqueryseyisuluView Answer on Stackoverflow
Solution 4 - JqueryMartlarkView Answer on Stackoverflow
Solution 5 - JqueryEyong Kevin EnowanyoView Answer on Stackoverflow
Solution 6 - JqueryThemisView Answer on Stackoverflow