如何将jquery变量传递给Python脚本?

时间:2013-10-16 10:40:55

标签: jquery python mongodb variables pymongo

我有一个html表单,带有jquery脚本以使其动态化。

我在debian wheezy下使用Apache2作为服务器,

MongoDB for database,

Pymongo作为司机,

最后,当表单完成时,所有字段都存储在我的.js文件中的变量中,其中json格式如下:

var payload = {"field1":"Richard","field2":"Berroy"}

当你点击提交按钮时,我使用ajax post功能:

$("#submitbtn").click( function() {
var payload = {"field1":"Richard","field2":"Berroy"}
$.post('python/mongo_brief_write.py', {bn: payload});

mongo_brief_write.py的目标是使用pymongo驱动程序将有效负载var的内容插入到MongoDB数据库的集合中。

这是我的这个python脚本的代码:

import pymongo
import json
import requests
from pymongo import Connection
//test = {"field1":"Richard","field2":"Berroy"}
chain = request.POST[bn]
con = Connection("mongodb://192.xxx.xx.xxx/")
db = con.central
collection = db.brief
post_id = collection.insert(chain)

如果我选择我的测试var它工作正常,但如果我评论测试并使用该请求行,我总是有问题,我现在不知道如何从我的jquery表单中获取该json对象,将其存储在数据库?

谢谢

1 个答案:

答案 0 :(得分:0)

尝试使用get之类的

chain = request.POST.get('bn')

使用

检查post variable对其进行测试
request.post_vars

Url可以帮助您Passing list of values to django view via jQuery ajax callGet data from POST ajax call using web2py