Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (45.3k points)

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 )

            }

        })

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.

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    

For argument's sake, let's 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'[email protected]', 

               u'postcode': u'A10 5BC', 

               u'address1': u'10 High Street', 

               u'second_name': u'Smith'

             }, 

 'customer_id': u'DaveSmithA10 5BCCat_Vegas1346244086'

}]

When I try to return this to the ajax call as

return results

I get the following error:

TypeError: 'list' object is not callable

Here is the traceback:

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?

Thanks

1 Answer

0 votes
by (16.8k points)

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)    

Related questions

Browse Categories

...