我想通过Ajax调用Twilio api,获取sms日志,然后将结果打印为HTML。我无法正确解析ajax调用的响应。
python代码是:
@application.route("/custom_log", methods=['POST'])
def customlog():
to_number = request.values.get('to_number', None)
user_id = session.query(User.id).filter(User.validation == 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
ajax代码是:
<script>
$(document).ready(function() {
$( "#custom_log_btn" ).click(function() {
alert('script')
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();
$.ajax({
url: '/custom_log',
data: { 'to_number': to_number },
type: 'POST',
success: function(msgs) {
alert(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>{{ m.to }} </td>';
html += '<td>{{ m.status }} </td>';
html += '</tr>';
html += '{% endfor %}</table>';
$("#result").html(html);
return false;
//});
},
error: function(error) {
console.log(error);
}
});
});
});
</script>
追溯是:
127.0.0.1 - - [21/May/2016 19:15:05] "GET /logs HTTP/1.1" 200 -
127.0.0.1 - - [21/May/2016 19:15:05] "GET /resources/demos/style.css HTTP/1.1" 404 -
delivered
delivered
127.0.0.1 - - [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/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Library/Python/2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Python/2.7/site-packages/flask/app.py", line 1478, in full_dispatch_request
response = self.make_response(rv)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1577, in make_response
rv = self.response_class.force_type(rv, request.environ)
File "/Library/Python/2.7/site-packages/werkzeug/wrappers.py", line 841, in force_type
response = BaseResponse(*_run_wsgi_app(response, environ))
File "/Library/Python/2.7/site-packages/werkzeug/test.py", line 867, in run_wsgi_app
app_rv = app(environ, start_response)
TypeError: 'list' object is not callable
我认为错误来自返回线。我已经尝试了返回'OK',返回m.status,这也不会产生预期的结果。我希望能够将结果从Twilio api调用传递回ajax函数,以便我可以打印它。我怎样才能做到这一点?
答案 0 :(得分:1)
确实,您不允许返回列表。返回json.dumps(msgs)
并在javascript中将其解析为JSON.parse(response)
。然后你将有一个javascript数组。