简体   繁体   中英

Why I can't get json data from django

My environment is : 1. server is apache 2.4 2. python 3.3, django 1.6 3. windows

django's views.py:

import simplejson as json
from django.views.generic import View
from django.views.decorators import csrf_exempt
from django.contrib import auth
from django.http import HttpResponse

class LoginFormView(View):
    template_name = 'game/login.html'

    @csrf_exempt
    def dispatch(self, *args, **kwargs):
        return super().dispatch(*args, **kwargs)

    def get(self, request, *args, **kwargs):
        return render(request, self.template_name, {})

    def post(self, request, *args, **kwargs):
        if request.is_ajax():
            username = request.POST['input_username']
            password = request.POST['input_password']
            user = auth.authenticate(username=username,
                                     password=password)
            payload = {}
            if user is not None and user.is_active:
                auth.login(request, user)
                payload['msg'] = 'successful login'
            else:
                payload['msg'] = 'login failed'
            return HttpResponse(json.dumps(payload),
                                mimetype = 'application/json')
        else:
            return HttpResponse("Error: Request is not through ajax")

My js file is:

$(document).ready(function() {
    $('#submit_btn').bind('click', function(){
        $.ajax({
            url: "/game/login/",
            data: $('#login_form').serialize(),
            dataType: "json",
            type: "POST",
            success: function(data){
                console.info(data);
                //do some things
            }
        })
    })
})

My problem is : django can get the username and password, user can pass the authentication, I want to return some message to the client, but the js code can't get the json data from django, in the firebug, the json data contains a record {“status”: "success"} or {“status”: "failure"}, I don't know where the {“status”: "success"} comes from ,I can't get the custom message, firebug always show me the "undefined". Is the Easyui's code problem? I need your help, thanks very much!

I got the answer. The reason is mod_wsgi's running mode. If use mod_wsgi's embeded mode, django's code is kept in RAM, the purpose is to improve speed. So once django's code is changed, we have to restart apache's http service.To avoid the situatioin, we could use mod_wsgi's deamon mode.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM