Flask Python,试图返回列表或dict到Ajax调用

时间:2012-08-30 08:40:37

标签: python ajax flask

在Flask应用中,我有以下ajax调用:

    $.ajax({
            url: "{{ url_for( 'bookings.get_customer' ) }}",
            type: "POST",
            data: nameArray,
            success: function( resp ){
                console.log( resp )
            }
        })

正如您所看到的,我正在传递一个数组,我将搜索我的mongo数据库,该数据库将返回或不返回客户。

因此,负责处理此ajax调用的python def是:

@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 results    

为了论证,让我们说customer_obj调用返回以下列表:

[{'customer': {
               u'first_name': u'Dave', 
               u'tel': u'0121212121458', 
               u'country': u'UK',     
               u'address2': u'Townington', 
               u'address3': u'Cityville', 
               u'email': u'dave@smith.com', 
               u'postcode': u'A10 5BC', 
               u'address1': u'10 High Street', 
               u'second_name': u'Smith'
             }, 
 'customer_id': u'DaveSmithA10 5BCCat_Vegas1346244086'
}]

当我尝试将此作为

返回到ajax调用时
return results

我收到以下错误:

 TypeError: 'list' object is not callable

这是追溯:

Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1701, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1689, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1687, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1361, in
full_dispatch_request
response = self.make_response(rv)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1450, in make_response
rv = self.response_class.force_type(rv, request.environ)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/wrappers.py", line 711, in
force_type
response = BaseResponse(*_run_wsgi_app(response, environ))
File "/usr/local/lib/python2.7/dist-packages/werkzeug/test.py", line 818, in
run_wsgi_app
app_iter = app(environ, start_response)
TypeError: 'list' object is not callable

有人有任何建议吗?

由于

3 个答案:

答案 0 :(得分:41)

Flask不希望您从视图函数返回list个对象。先尝试jsonify

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)    

答案 1 :(得分:10)

josonify works..but如果你打算只传递一个没有'results'键的数组,你可以使用python中的json库。以下转换适用于我..

 import json

 @app.route('/test/json')
 def test_json():
 list = [
        {'a': 1, 'b': 2},
        {'a': 5, 'b': 10}
       ]
 return json.dumps(list)

答案 2 :(得分:5)

你需要做

return jsonify(result=your_result)

另请查看documentation,这可能非常有帮助。