瓶的Python,试图返回列表或字典,以Ajax调用字典、列表、Python、Ajax

2023-09-10 16:39:29 作者:君临天下

在烧瓶中的应用程序,我有以下的Ajax调用:

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

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

正如你所看到的,我传递一个数组反对,我将寻找我的Mongo的数据库,这将返还或不返回客户。

As you can see, I am passing an array against which I will search my mongo database, which will either return or not return a customer.

所以,蟒蛇高清,负责处理这Ajax调用是:

So, the python def that is responsible for handling this ajax call is:

@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调用返回清单如下:

For argument's sake, lets say the customer_obj call returns the following list:

[{'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调用为

When I try to return this to the ajax call as

return results

我收到以下错误:

I get the following error:

 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

有没有人有什么建议?

Does anyone have any suggestions?

感谢

推荐答案

瓶不指望你会返回列表从您的视图函数对象。尝试 jsonify 之前:

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)