【发布时间】:2016-03-27 15:29:28
【问题描述】:
我正在使用 Python 开发一个应用引擎项目,将数据从 html 页面(使用 jQuery)发送到 Python。
我正在使用这个 jQuery 插件将 HTML 表序列化为 javascript 对象:https://github.com/lightswitch05/table-to-json
function addFood() {
var table = $('#planTable').tableToJSON();
var info = {
"user": "{{user}}",
"plan": JSON.stringify(table)
};
$.ajax({
type: "POST",
url: "/addplan",
dataType: "json",
data: JSON.stringify(info)
})
.done(function(msg) {
alert(msg.text);
});
}
在 Python 中我有那个代码:
class AddPlanToDB(webapp2.RequestHandler):
def post(self):
info = json.loads(self.request.body) #retrive data from jQuery
print info
user = info['user']
template_values = {
'text': 'All done!'
}
self.response.out.write(json.dumps(template_values))
打印 info 给我输出:
{u'plan': u'[{"Food":"Cheese, fontina","Grams":"100g","Pro":"25.6","Carbo":"1.6","Fat":"31.1","":""},{"Food":"Butter, salted","Grams":"50g","Pro":"0.4","Carbo":"0.0","Fat":"40.6","":""},{"Food":"Corn bran, crude","Grams":"200g","Pro":"16.7","Carbo":"171.3","Fat":"1.8","":""},{"Food":"","Grams":"Total:","Pro":"42.7","Carbo":"172.9","Fat":"73.5"}]', u'user': u'test@example.com'}
info['user'] 返回有关用户的正确信息。 但我无法检索有关
的数据食物:
克:
专业版:
碳水化合物:
脂肪:
谁能告诉我如何在 Python 中获取这些数据?
提前谢谢我的小英语。
【问题讨论】:
标签: javascript jquery python json google-app-engine