【发布时间】:2013-10-24 10:38:19
【问题描述】:
我有一个 html 表单,带有一个 jquery 脚本来使其动态化。
我在debian wheezy下使用Apache2作为服务器,
用于数据库的 MongoDB,
Pymongo 作为驱动程序,
最后,当表单完成时,所有字段都存储在我的.js文件中的一个变量中,json格式如下:
var payload = {"field1":"Richard","field2":"Berroy"}
当你点击提交按钮时,我使用了一个 ajax 发布功能:
$("#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)
如果我选择我的测试变量,它可以正常工作,但是如果我评论测试并使用该请求行,我总是会遇到问题,我现在不知道如何从我的 jquery 表单中获取该 json 对象,并将其存储在数据库?
谢谢
【问题讨论】:
-
你得到的错误信息是什么?顺便说一句,你应该使用
#来评论python代码。 -
嗨 Leon,你是正确的 # 我有
POST http://192.168.15.200/central/python/mongo_brief_write.py 500 (Internal Server Error) jquery-1.8.3.js:8434 send jquery-1.8.3.js:8434 jQuery.extend.ajax jquery-1.8.3.js:7986 (anonymous function) central.js:910 jQuery.event.dispatch jquery-1.8.3.js:3058 elemData.handle.eventHandle
标签: jquery python mongodb variables pymongo