我的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')