tags:

views:

76

answers:

4
$.getJSON('/jsonView', {
                                tag: "userName",
                                userName: 'zjm1126'
                            },
                            function (H) {
                                if (H.result == "successName") {
                                    F.showOk(h.ok);
                                } else {
                                    if (H.result == "existName") {
                                        F.showErr(h.userNameExist);
                                    }
                                }
                            })

the view:

def jsonView(request):
    #i don't know,can you.
+1  A: 

I suggest you work your way through James Bennett's tutorial on using AJAX with Django - he includes details on writing views that return JSON.

Dominic Rodger
+1 for the good link
Flávio Amieiro
A: 

It's pretty simple:

  1. Retrieve GET data from request.GET
  2. Do stuff
  3. Return a response with mimetype application/javascript

1 and 2 are like any other view. If you don't know how to do 3, check out django.http.HttpResponse.__init__

ozan
A: 

This is relatively straightforward

def json_view(request):
  username=request.GET.get('username')
  result='successName'
  if username:
    try:
      user=User.objects.get(username=username)
      result='existName'
    except User.DoesNotExist:
      pass
    return HttpResponse(simplejson.dumps({'result': result}))
czarchaic
hi czarchaic, see http://stackoverflow.com/questions/2092566/how-do-i-write-this-django-viewjsonview-it-is-used-getjsonjquery/2098847#2098847
zjm1126
hi czarchaic,see http://stackoverflow.com/questions/2099345/why-1-is-ok-but-2-is-error-use-django-and-jquery the email is not ok.thanks,
zjm1126
A: 
def json_view(request):
  import json
  username=request.GET.get('username')
  result='successName'
  if username:
    try:
      user=User.objects.get(username=username)
      result='existName'
    except User.DoesNotExist:
      pass
    return HttpResponse(json.dumps({'result': result}))

it makes 500 INTERNAL SERVER ERROR.

why???

zjm1126
Do you have DEBUG on? What type of errors are you getting? I suspect json is not being imported. Try changing it to `from django.utils import simplejson as json`
czarchaic