这是我的代码:
网址.py:
from django.conf.urls import patterns
from views import show,showpage
urlpatterns = patterns('',
(r'^$',showpage),
(r'^show/$',show),
)
视图.py
from django.http import HttpResponse
def show(request):
arr = reqeust.GET.get('arr','Nothing')
print arr#**it's 'Nothing'**
if arr == 'Nothing':
msg = 'fail'
else:
msg = 'pass'
return HttpResponse(msg)
def showpage(request):
html = ''' <html>
<head>
<script language="javascript" type="text/javascript" src="/site_media/js/jquery-1.7.2.js"></script>
</head>
<body>
<input type="button" id="input_btn"/>
</body>
<script language="javascript" type="text/javascript">
$(function(){
$("#input_btn").bind("click",function(){
arr = [1,2,3,4,5,6]
$.ajax({
type:"GET",
url:"/show/",
data:{arr:arr},//**I want pass the arr to django**
success:function(data){alert(data);},
error:function(data){alert(data);}
});
});
})
</script>
</html>'''
return HttpResponse(html)
当我访问localhost:8000并单击按钮时,我在后端控制台中打印了“Nothing”,如您所知,我在前端收到消息“失败”
呃..任何人都可以解决这个问题,我想要代码(arr = reqeust.GET.get('arr','Nothing')
)返回一个列表([1,2,3,4,5,6]
)或类似的东西
---------附加1------
当我单击 btn 时,我在控制台中得到了这个
[12/Jun/2012 09:48:44] “GET /show/?arr%5B%5D=1&arr%5B%5D=2&arr%5B%5D=3&arr%5B%5D=4&arr%5B%5D=5&arr% 5B%5D=6 HTTP/1.1" 200 4
---------追加2--------
我像这样修复了我的代码:
arr = request.GET.get('arr[]','Nothing')
我得到了 6,这是 javascript 数组的最后一个,我怎样才能得到整个数组?
提前谢谢!