1

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

I use Apache2 under debian wheezy for the server,

MongoDB for database,

Pymongo as driver,

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"}

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});

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.

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)

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 ?

Thank you

Manuhoz
  • 89
  • 2
  • 12
  • What's the error information did you get? And by the way, you should use `#` to comment python code. – Leon Young Oct 16 '13 at 10:48
  • Hi Leon, you're right for the # I Have `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` – Manuhoz Oct 18 '13 at 10:04

1 Answers1

0

Try to use get like

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

to check your post variable test it by using

request.post_vars

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

Community
  • 1
  • 1
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
  • Thank you Rohan, I don't understand why I need to use wep2py, Django or pybottle to perform a simple action. I mean on the other way it is that simple, I have a python script with pymongo that read my database and pass the content as a json object in my jquery environnement for display purpose without a third party program. Now I just want to write in my mongodb the content of the form and it is different ? – Manuhoz Oct 18 '13 at 10:09
  • I've tried your request above and I have the same error, should I import something in my python script ? – Manuhoz Oct 18 '13 at 10:10