Flask Python, trying to return list or dict to Ajax call

后端 未结 3 1841
不知归路
不知归路 2021-02-05 04:21

Within a Flask app, I have the following ajax call:

    $.ajax({
            url: \"{{ url_for( \'bookings.get_customer\' ) }}\",
            type: \"POST\",
            


        
3条回答
  •  北荒
    北荒 (楼主)
    2021-02-05 04:40

    Flask doesn't expect that you will return list object from your view function. Try jsonify it before:

    from flask import jsonify
    
    @bookings.route( '/get_customer', methods=[ 'POST' ] )
    def get_customer():
        name = {}
        for key, value in request.form.items():
            name[ key ] = value
    
        customer_obj = customer_class.Customer()
        results = customer_obj.search_customer( name )
    
        return jsonify(customers=results)    
    

提交回复
热议问题