2012-06-12 9 views
5

qui è il mio codice:ottenere l'array JavaScript Django

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) 

quando accedo localhost: 8000, e fare clic sul pulsante, ho stampato 'Nothing' nella console di back-end, come sai che ha ottenuto il messaggio 'fallire' nel frontend

uh..can nessuno sovle questo problema, voglio ottenere il codice (arr = reqeust.GET.get('arr','Nothing')) che restituiscono una lista ([1,2,3,4,5,6]) o qualcosa di simile

------ --- append1 -------

ho ottenuto questo nella console quando clicco il btn

[12/Giu/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

Ho fissato il mio codice come questo:

arr = request.GET.get('arr[]','Nothing') 

ed ho ottenuto 6, che è l'ultimo della matrice javascript, come posso ottenere l'intero array?

grazie anticipo!

+0

Che aspetto ha l'URL della richiesta reale? –

+0

scusa! Ho dimenticato di tornare alla pagina html, aspetta un minuto – sashimi

+0

@Ignacio Vazquez-Abrams Ho risolto la mia domanda e ho inserito l'URL di richiesta nella parte inferiore del contenuto della domanda – sashimi

risposta

10

jQuery ha deciso di aggiungere le parentesi quadre al nome del campo, molto probabilmente per ospitare PHP. Non sono richiesti per Django, ma qualunque cosa. Accedi invece a request.GET['arr[]'] e request.GET.getlist('arr[]').

+0

** request.GET.getlist ('arr []') ** lavori!! e ho aggiunto informazioni sull'URL della richiesta ** arr% 5B% 5D = 1 & arr% 5B% 5D = 2 ** EQUALS ** arr [] = 1 & arr [] = 2 **. e ** request.GET ** restituisce ** QueryDict ** grazie @ Ignacio Vazquez-Abrams^_^ – sashimi

+0

che bella risposta. –

5

è anche possibile impostare:

$.ajaxSettings.traditional = true; 

Questo renderà jQuery comportarsi secondo lo standard HTTP reale, che lavora con Django.

Questa operazione è necessaria quando non hai intenzione di accedere request.GET o request.POST te (es. Ajax-pubblicazione di un modulo).

Si consiglia di impostare questo in ogni progetto django.

+0

ringraziamento @jpic lo stesso, cerco il tuo suggerimento e ho la richiesta URL come questo: ** http: //127.0.0.1: 8000/show/arr = 1 & arr = 2 & arr = 3 & arr = 4 & arr = 5 & arr = 6 **. Cambio ** request.GET.getlist ('arr') **, anch'io lavoro.grazie del tuo suggeistion ~ – sashimi