[英]Issue with my Django request from a JSON POST
I try to access to a JSON object, sent from JS-jquery with $.post to a Django script, 我尝试访问一个JSON对象,该对象从JS-jquery用$ .post发送到Django脚本,
I tried a combination on many things I saw on Stackoverflow, but I cant make it work : 我尝试了在Stackoverflow上看到的许多事情的组合,但是我无法使其工作:
On the .js side : 在.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.");
});
and the content of my script named mongo_brief_write.py is : 我的脚本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
If I press the submit button, I have the "Done" alert displayed correctly, but nothing in my collection in my mongoDB. 如果按提交按钮,则将正确显示“完成”警报,但是mongoDB中的集合中没有任何内容。
If I replace t by test in 如果我用测试代替t
post_id = collection.insert(test)
I have the done alert too and my object is created in my mongo DB collection. 我也有完成警报,并且在mongo DB集合中创建了我的对象。
Where is my mistake ? 我的错误在哪里? in my POST request ?
在我的POST请求中? I work under Apache and I use modpython.
我在Apache下工作,并且使用modpython。
Looks like its happens because of python namespace rules. 看起来是由于python名称空间规则导致的。 If you define variable in function:
如果在函数中定义变量:
>>>def random_func(input):
t = input
return t
>>>t
Traceback (most recent call last): File "<input>", line 1, in <module>
NameError: name 't' is not defined
Its won't be global variable. 它不会是全局变量。 So, what you need to do is too ways: first, put code with base manipulation in function save_events_json:
因此,您需要做的是:首先,将具有基本操作的代码放在函数save_events_json中:
def save_events_json(request):
t = request.raw_post_data
con = Connection("mongodb://xxx.xxx.xxx.xxx/")
db = con.central
collection = db.brief
test = {"name":"robert","age":"18"}
post_id = collection.insert(t)
from django.http import HttpResponse
return HttpResponse(content=t)
or set the variable "t" global: 或全局设置变量“ t”:
def save_events_json(request):
global t
t = request.raw_post_data
return t
Dear @Kyrylo Perevozchikov, I've updated my code : 亲爱的@Kyrylo Perevozchikov,我已经更新了我的代码:
import pymongo
from pymongo import Connection
from django.utils import simplejson as json
from django.http import HttpResponse,HttpRequest
request = HttpRequest()
if request.method == 'POST':
def index(request):
global t
t = request.raw_post_data
post_id=Connection("mongodb://xxx.xxx.xxx.xxx/").central.brief.insert(t)
return HttpResponse(content=t)
else:
def index(req):
s="Not A POST Request"
return s
When I click on the jquery button I have the "Not A POST Request" 当我单击jquery按钮时,我收到“不是POST请求”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.