如何将 jquery 变量传递给 Python 脚本? [英] How to pass a jquery variable to a Python script?

查看:54
本文介绍了如何将 jquery 变量传递给 Python 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an html form, with a jquery script to make it dynamic.

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

I use Apache2 under debian wheezy for the server,

MongoDB 用于数据库,

MongoDB for database,

Pymongo 作为司机,

Pymongo as driver,

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

At the end, when the form is completed, all the fields are stored in a variable in my .js file, with a json format as follow :

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

当你点击提交按钮时,我使用了一个 ajax 发布功能:

When you click on the submit button, I use an ajax post function :

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

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

The goal of mongo_brief_write.py is to insert the content of the payload var into a collection of my MongoDB database, using pymongo driver.

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

here is my code for this python script :

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 对象,将其存储在数据库?

If I choose my test var it works fine, but I always have an issue if I comment test and use that request line, I don't now how to get that json object from my jquery form, to store it in the database ?

谢谢

推荐答案

尽量使用 get like

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

检查您的 post 变量 使用

request.post_vars

Url 可能会帮助您 传递列表通过 jQuery ajax 调用从使用 web2py POST ajax 调用

Url's may help you Passing list of values to django view via jQuery ajax call and Get data from POST ajax call using web2py

这篇关于如何将 jquery 变量传递给 Python 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆