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

编程入门 行业动态 更新时间:2024-10-27 20:26:35
本文介绍了如何将 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 脚本?

本文发布于:2023-11-23 18:15:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622420.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   如何将   脚本   jquery   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!