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
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With