【问题标题】:How to pass a jquery variable to a Python script?如何将 jquery 变量传递给 Python 脚本?
【发布时间】: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


【解决方案1】:

尽量用get点赞

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

检查您的post variable 使用测试它

request.post_vars

网址Passing list of values to django view via jQuery ajax callGet data from POST ajax call using web2py 可能会对您有所帮助

【讨论】:

  • 谢谢 Rohan,我不明白为什么我需要使用 wep2py、Django 或 pybottle 来执行一个简单的操作。我的意思是另一方面,它就是这么简单,我有一个带有 pymongo 的 python 脚本,它读取我的数据库并将内容作为 json 对象传递到我的 jquery 环境中,用于在没有第三方程序的情况下进行显示。现在我只想在我的 mongodb 中写入表单的内容,它是不同的?
  • 我已经尝试了你上面的请求,我有同样的错误,我应该在我的 python 脚本中导入一些东西吗?
猜你喜欢
  • 2011-12-03
  • 2021-08-14
  • 1970-01-01
  • 2015-06-25
  • 1970-01-01
  • 2020-05-20
  • 1970-01-01
  • 2010-09-09
  • 1970-01-01
相关资源
最近更新 更多