Python 无法在Django中的Jquery ajax post调用上获取视图上的值
单击按钮后,我进行Jquery Ajax post调用,但在视图中它并没有获取值。代码如下: jquery:-Python 无法在Django中的Jquery ajax post调用上获取视图上的值,python,django,jquery,Python,Django,Jquery,单击按钮后,我进行Jquery Ajax post调用,但在视图中它并没有获取值。代码如下: jquery:- $("#startSimulator").click(function(){ test = $("#testRange").val(); gameKey = ($('input:radio[name=radioGroup]:checked').val() || 0); $("#imageLoading").css("display"
$("#startSimulator").click(function(){
test = $("#testRange").val();
gameKey = ($('input:radio[name=radioGroup]:checked').val() || 0);
$("#imageLoading").css("display", "block");
$.post("/blackout/setup/",{d : d,test : test,gameKey : gameKey}, function(data){
$("#result").append(data);
});
});
URL.py:
urlpatterns = patterns('',
(r'^blackout/setup/$', 'blackout.views.setup'),
)
views.py:
def setup(request):
gameKey = request.POST['gameKey']
test = request.POST['test']
data = request.POST['d']
#Some other code
#And then HttpResponse(data)
错误:-
Exception Value:
"Key 'gameKey' not found in <QueryDict: {}>"
/home/dhruv/blackout_new_project/blackout/blackout_proj/blackout/views.py in setup
gameKey = request.POST['gameKey']
异常值:
“在中找不到密钥“gameKey”
/home/dhruv/blackout_new_project/blackout/blackout_proj/blackout/views.py在设置中
gameKey=request.POST['gameKey']
这到底应该做什么?我认为这可能是问题所在,我不确定RadioBox是否正确,但如果未设置复选框,则其checked属性上的复选框返回null或undefined(不记得是哪一个)
在进行ajax调用之前检查gameKey的值。这不会产生任何问题,它会从一堆单选按钮中获取选中单选按钮的值。问题是在后端(views.py)没有接收到这些值。在进行AJAX调用之前,我已经检查了是否收到了这些值。我认为问题在于您的数据dict
{d:d,test:test,gameKey:gameKey}
您可以尝试一下这个{'d':d',test':test,'gameKey':gameKey}
gameKey = ($('input:radio[name=radioGroup]:checked').val() || 0);