here is my code:
这是我的代码:
urls.py:
from django.conf.urls import patterns
from views import show,showpage
urlpatterns = patterns('',
(r'^$',showpage),
(r'^show/$',show),
)
views.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)
when I access localhost:8000,and click the button,I got 'Nothing' printed in the backend console,as you know I got message 'fail' in the frontend
当我访问localhost:8000,然后单击按钮时,我在后端控制台中打印出“Nothing”,因为您知道我在前端收到消息“fail”
uh..can anybody sovle this problem,I want get the code (arr = reqeust.GET.get('arr','Nothing')
) which return a list([1,2,3,4,5,6]
) or something like this
呃..可以解决这个问题,我想得到一个返回列表的代码(arr = reqeust.GET.get('arr','Nothing'))([1,2,3,4,5,6] )或类似的东西
---------append1-------
I got this in the console when I click the btn
当我点击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
[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
---------apend2-------
I fixed my code like this:
我修改了我的代码:
arr = request.GET.get('arr[]','Nothing')
and I got 6 which is the last one of the javascript array,how can I get the whole array?
我得到6这是javascript数组的最后一个,我怎么能得到整个数组?
thanks advance!
2 个解决方案
#1
10
jQuery has decided to add square brackets to the field name, most likely to accommodate PHP. They aren't required for Django, but whatever. Access request.GET['arr[]']
and request.GET.getlist('arr[]')
instead.
jQuery决定在字段名称中添加方括号,最有可能容纳PHP。它们不是Django所必需的,但无论如何。访问request.GET ['arr []']和request.GET.getlist('arr []')代替。
#2
5
You can also set:
你也可以设置:
$.ajaxSettings.traditional = true;
This will make jQuery behave according to the actual HTTP standard, which works with Django.
这将使jQuery的行为符合实际的HTTP标准,该标准适用于Django。
This is required when you're not going to access request.GET or request.POST yourself (ie. ajax-posting a form).
当您不打算自己访问request.GET或request.POST时(即ajax发布表单),这是必需的。
It's definitively recommended to set this in every django project.
绝对建议在每个django项目中设置它。
#1
10
jQuery has decided to add square brackets to the field name, most likely to accommodate PHP. They aren't required for Django, but whatever. Access request.GET['arr[]']
and request.GET.getlist('arr[]')
instead.
jQuery决定在字段名称中添加方括号,最有可能容纳PHP。它们不是Django所必需的,但无论如何。访问request.GET ['arr []']和request.GET.getlist('arr []')代替。
#2
5
You can also set:
你也可以设置:
$.ajaxSettings.traditional = true;
This will make jQuery behave according to the actual HTTP standard, which works with Django.
这将使jQuery的行为符合实际的HTTP标准,该标准适用于Django。
This is required when you're not going to access request.GET or request.POST yourself (ie. ajax-posting a form).
当您不打算自己访问request.GET或request.POST时(即ajax发布表单),这是必需的。
It's definitively recommended to set this in every django project.
绝对建议在每个django项目中设置它。