我有一個簡單的函數,它應該返回JSON。由Flask返回空JSON
@app.route('/storage/experiments', methods=['GET'])
def get_experiments():
if not request.json:
abort(400)
experiments = db['experiments']
cursor = experiments.find(request.get_json())
print(dumps(cursor))
resp = Response(response=dumps(cursor),
status=200, \
mimetype="application/json")
return resp
打印(轉儲(光標))示出了
[{"current": "11", "date": "12.12.2001", "_id": {"$oid": "551c7b642349c517f5fa5223"}, "name": "xaxa", "voltage": "34"}]
但返回空方括號[]
同時創造自己的Response對象提供更多的靈活性,你試圖簡單地返回jsonify(轉儲(光標))? – SimplicityGuy 2015-04-05 03:17:08
是的,我試着jsonify(轉儲(光標))返回500錯誤, jsonify(list(cursor))return {}, jsonify(cursor)return {}。 我沒有更多的變體 – Alexander1 2015-04-05 12:21:02
只是一個快速猜測,但它是否工作,如果您刪除/註釋掉打印語句? – 2015-04-05 12:41:45