【发布时间】:2015-07-25 16:25:13
【问题描述】:
我是 Django 新手。我正在向 python 函数发送一个 ajax POST 请求,该函数又将数据存储在来自 ajax 的变量中并返回 HttpResponse。所以我在 python 中检查了 request.method,它以 GET 形式出现。
$.ajax({
url:"create_post/",
type:"POST",
data : {F_Name: First_Name ,L_Name: Last_name,Eadd: Email , Password: Pass},
success:function(){
window.location.href="create_post/";
console.log ("Success")
},
cache:false,
failure: function(errMsg) {
alert(errMsg);
}
});
这是我的 ajax 请求。
它将数据发送到这个函数。
def create_post(request):
if request.method == 'GET':
First_name=request.GET['F_Name'];
Last_Name=request.GET["L_Name"];
Email_address=request.GET["Eadd"];
Pass=request.GET["Password"];
return HttpResponse("<html><body>hi this is me .</body></html>");
当我检查为return(request.method) 时,它给了我GET。
有人可以解释这种行为吗?
在函数中我有request.method==GET 在这种情况下DJango 给我内部服务器500 错误。
谢谢
【问题讨论】:
-
我仍然收到相同的 GET 请求,我将
type替换为method -
如果您收到 500 错误,您确定它正在发送 GET 请求吗?你能显示你的 URL 配置吗?
-
在views.py中我检查了它...
def create_post(request): return HttpResponse(request.method)我得到了GET。所以这意味着它是一个GET请求? -
在您的浏览器开发者选项中检查触发的请求是作为 POST 还是 GET(在浏览器端)发送。
-
你可以
print request.method看看你的控制台是打印POST还是GET