1

我的ajax调用有问题,成功没有按预期工作,我检查了控制台,服务器端的脚本正在检索和回答,我还进行了console.debug调用以在控制台显示答案并没有发生。阅读恐惧后,我找不到错误。你能帮我解决这个问题吗?

我的阿贾克斯

$.ajax({
            url: '{% url 'credentials.ajax.users_save' %}',
            type: 'post',
            dataType: 'json',
            data: {
                user_id: $('#user_id').val(),
                username: $('#username').val(),
                password: $('#password').val(),
                last_name: $('#last_name').val(),
                first_name: $('#first_name').val(),
                email: $('#email').val(),
                address: $('#address').val(),
                phone_office: $('#phone_office').val(),
                phone_home: $('#phone_home').val(),
                expiration: $('#expiration').val(),
            },
            success: function (msg) {
                console.debug(msg)
                if ( msg == false || parseInt(msg) == 0) {
                    $.pnotify({
                        title: '{% trans 'Error' %}',
                        text: '{% trans 'There were a error, please try again' %}',
                        type: 'error'
                    });

                } else {
                    $.pnotify({
                        title: '{% trans 'Congrats' %}',
                        text: '{% trans 'User has been create' %}',
                        type: 'success'
                    });
                    $('#modalCreateUser').modal('hide');
                    window.setTimeout(function(){location.reload()}, 2000)

                }
            }
        });

我的服务器端功能

def users_save (request):
    try:
        if request.is_ajax() and request.POST:
            user_id = request.POST['user_id']
            s = get_current_site(request)
            print s.id
            u = User (
                      username = request.POST['username'],
                      first_name = request.POST['first_name'],
                      last_name = request.POST['last_name'],
                      email = request.POST['email']
                      )
            u.set_password(request.POST['password'])
            up = UserProfile(
                      address = request.POST['address'],
                      phone_home = request.POST['phone_home'],
                      phone_office = request.POST['phone_office']
                             )
            up.user = u
            up.site = s
            #up.save()
            u.save()
            return HttpResponse(True, mimetype='application/json')
    except:
        return HttpResponse(False, mimetype='application/json')
4

2 回答 2

1

看来您有一些语法错误....我在下面的代码中用注释标记了它们

$.ajax({
        url: '{% url 'credentials.ajax.users_save' %}',
        type: 'post',
        dataType: 'json',
        data: {
            user_id: $('#user_id').val(),
            username: $('#username').val(),
            password: $('#password').val(),
            last_name: $('#last_name').val(),
            first_name: $('#first_name').val(),
            email: $('#email').val(),
            address: $('#address').val(),
            phone_office: $('#phone_office').val(),
            phone_home: $('#phone_home').val(),
            expiration: $('#expiration').val() //, no comma here
        },
        success: function (msg) {
            console.debug(msg); // needed a semi-colon
            if ( msg == false || parseInt(msg) == 0) {
                $.pnotify({
                    title: '{% trans 'Error' %}',
                    text: '{% trans 'There were a error, please try again' %}',
                    type: 'error'
                });

            } else {
                $.pnotify({
                    title: '{% trans 'Congrats' %}',
                    text: '{% trans 'User has been create' %}',
                    type: 'success'
                });
                $('#modalCreateUser').modal('hide');
                window.setTimeout(function(){location.reload()}, 2000); // needed a semicolon

            }
        }
    });
于 2013-06-27T23:31:10.663 回答
0

我注意到我在回答

return HttpResponse(False, mimetype='application/json')

代替

return HttpResponse(0, mimetype='application/json')

由于 True 是 django sintaxis,导致 javascript 和 jquery 无法解析它,然后它不知道该怎么做,我已经改变了它,现在它工作得很好

非常感谢

于 2013-06-27T23:39:05.843 回答