user1903663 user1903663 - 5 months ago 62x
Ajax Question

using Python, Flask and Ajax, how can I parse a response from an ajax call and print as HTML?

I want to make a call to Twilio api, via Ajax, and get sms logs and then print the results as HTML. I cannot parse the response, from the ajax call, correctly.

The python code is:

@application.route("/custom_log", methods=['POST'])
def customlog():
to_number = request.values.get('to_number', None)
user_id = session.query( == request.cookies.get("validation")).scalar()
sub_account_sid = session.query(Credential.twilio_id).filter(Credential.user_id == user_id).scalar()
sub_account_auth_token = session.query(Credential.twilio_token).filter(Credential.user_id == user_id).scalar()
client = TwilioRestClient(sub_account_sid, sub_account_auth_token)
msgs = client.messages.list(to=to_number)
for m in msgs:
print m.status
return msgs

The ajax code is:

$(document).ready(function() {
$( "#custom_log_btn" ).click(function() {
var to_number = $('#to_number').val();
//var date_from = $('#datepicker_from').datepicker({ dateFormat: 'yyyy-mm-dd' }).val();
//var date_to = $('#datepicker_to').datepicker({ dateFormat: 'yyyy-mm-dd' }).val();
url: '/custom_log',
data: { 'to_number': to_number },
type: 'POST',
success: function(msgs) {
var html = '<table class="table table-striped table-hover">{% for m in msgs %}';
html += '<tr>';
html += '<td><a href="/detail?sid="{{ m.sid }}">Detail</a></td>';
html += '<td>{{ m.date_sent }}</td>';
html += '<td>{{ m.from_ }} </td>';
html += '<td>{{ }} </td>';
html += '<td>{{ m.status }} </td>';
html += '</tr>';
html += '{% endfor %}</table>';

return false;
error: function(error) {

The traceback is: - - [21/May/2016 19:15:05] "GET /logs HTTP/1.1" 200 - - - [21/May/2016 19:15:05] "GET /resources/demos/style.css HTTP/1.1" 404 -
delivered - - [21/May/2016 19:15:20] "POST /custom_log HTTP/1.1" 500 -
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/flask/", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/Library/Python/2.7/site-packages/flask/", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Library/Python/2.7/site-packages/flask/", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Library/Python/2.7/site-packages/flask/", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Python/2.7/site-packages/flask/", line 1478, in full_dispatch_request
response = self.make_response(rv)
File "/Library/Python/2.7/site-packages/flask/", line 1577, in make_response
rv = self.response_class.force_type(rv, request.environ)
File "/Library/Python/2.7/site-packages/werkzeug/", line 841, in force_type
response = BaseResponse(*_run_wsgi_app(response, environ))
File "/Library/Python/2.7/site-packages/werkzeug/", line 867, in run_wsgi_app
app_rv = app(environ, start_response)
TypeError: 'list' object is not callable

I assume that the error arises from the return line. I have experimented with return 'OK', return m.status and this does not produce the desired result either. I want to be bale to pass the result from the Twilio api call back to the ajax function so I can print it. How can I achieve this?


Indeed, you're not allowed to return a list. Return json.dumps(msgs) and in the javascript parse it as JSON.parse(response). Then you will have a javascript array.