首先,请原谅我的无知,我对此很陌生。
我的问题是我试图将存储在坐标mongodb中的json数据发送到客户端浏览器。我有一个使用Twitter的Streaming API将数据存储到数据库中的python模块。这可以正常工作,但是当我尝试将其发送到客户端时,它什么也没显示,尽管我可以看到服务器终端获取了更多数据。我以前没有使用过Flask或JQuery,因此基于http://flask.pocoo.org/docs/patterns/jquery/上的示例。
这是我的代码:
from flask import Flask, jsonify, render_template, request
from pymongo import Connection
app = Flask(__name__)
@app.route('/_reader')
def reader():
db = Connection().tstream
coll = db.tweets_tail
cursor = coll.find({"coordinates.type" : "Point" }, {"coordinates" :1},tailable=True,timeout=False)
ci=0
while cursor.alive:
try:
doc = cursor.next()
ci += 1
print doc
print ci
except StopIteration:
pass
return jsonify(ci, doc)
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True, port= 8888)
这是我的html客户端:
{% extends "layout.html" %}
{% block body %}
<script type=text/javascript>
$(function() {
$.getJSON($SCRIPT_ROOT + '/_reader',
function(data) {
$("#result").text(data.result);
});
return false;
});
</script>
<h1>Coordinates</h1>
<p>
<span id=result>?</span>
{% endblock %}
我希望收到新的坐标数据,并将其推送到客户端。
希望有人能帮忙。
谢谢
最佳答案
不确定您的线路
返回jsonify(ci,doc)
您确定返回正确吗?参数应为字典格式。你可以试试这个吗
return jsonify(dict(ci=ci, doc=doc))