Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

ajax

flask

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, 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'[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

like image 758
Ste77 Avatar asked Aug 30 '12 08:08

Ste77


People also ask

Can Ajax use python flask?

AXJS(Asynchronous JavaScript and XML) is Asynchronous, so a user can continue to use the application while the client program requests information from the server in the background. Now we'll use jQuery AJAX to post the form data to the Python Flask method.


1 Answers

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)     
like image 161
Alexey Kachayev Avatar answered Oct 11 '22 18:10

Alexey Kachayev