【发布时间】:2014-03-22 20:38:49
【问题描述】:
我尝试访问一个 JSON 对象,从 JS-jquery 使用 $.post 发送到 Django 脚本,
我尝试了我在 Stackoverflow 上看到的许多东西的组合,但我无法让它发挥作用:
在 .js 方面:
$("#submitbtn").click(function() {
var payload = {"name":"Richard","age":"19"};
$("#console").html("Sending...");
$.post("/central/python/mongo_brief_write.py",{'data': JSON.stringify(payload)},
function(ret){alert(ret);});
$("#console").html("Sent.");
});
我的脚本名为 mongo_brief_write.py 的内容是:
#!/usr/bin/env python
import pymongo
from pymongo import Connection
from django.utils import simplejson as json
def save_events_json(request):
t = request.raw_post_data
return t
con = Connection("mongodb://xxx.xxx.xxx.xxx/")
db = con.central
collection = db.brief
test = {"name":"robert","age":"18"}
post_id = collection.insert(t)
def index(req):
s= "Done"
return s
如果我按下提交按钮,我会正确显示“完成”警报,但我的 mongoDB 中的集合中没有任何内容。
如果我用 test in 替换 t
post_id = collection.insert(test)
我也有完成警报,我的对象是在我的 mongo DB 集合中创建的。
我的错误在哪里?在我的 POST 请求中?我在 Apache 下工作,我使用 modpython。
【问题讨论】:
标签: jquery python json django pymongo