现有电子邮件检查(Django&x2B;Javascript/Ajax)

现有电子邮件检查(Django&x2B;Javascript/Ajax),javascript,jquery,json,ajax,django,Javascript,Jquery,Json,Ajax,Django,我一直在尝试用Django和Javascript/Ajax构建一个电子邮件验证程序,但我被卡住了。我得到了Ajax的响应 {response: "This field is required.", email: false} email: false response: "This field is required." 但它总是一样的,即使我填写了电子邮件字段 这是我的视图。py @requires_csrf_token def email_check(request): email

我一直在尝试用Django和Javascript/Ajax构建一个电子邮件验证程序,但我被卡住了。我得到了Ajax的响应

{response: "This field is required.", email: false}
email: false
response: "This field is required."
但它总是一样的,即使我填写了电子邮件字段

这是我的视图。py

@requires_csrf_token
def email_check(request):
    email = request.POST.get('email', False)
    if request.is_ajax():
        if email:
            query_email = CustomUser.objects.filter(email=email)
            if query_email.exists():
                res = "{0} is already in use.".format(email)
            else:
                res = "This E-mail is ok."
            ajax_vars = {'response': res, 'email': email}
            json_data = json.dumps(ajax_vars)
        else:
            res = "This field is required."
            ajax_vars = {'response': res, 'email': email}
            json_data = json.dumps(ajax_vars)

        return HttpResponse(json_data, content_type='application/json')
这是模板脚本

<form>
    {% csrf_token %}
    <input id="email" type="email" name="email">E-mail</a>
</form>

<script type="text/javascript">

function getCookie(name) {
    var cookieValue = null;
    if (document.cookie && document.cookie != '') {
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) {
            var cookie = jQuery.trim(cookies[i]);
            // Does this cookie string begin with the name we want?
            if (cookie.substring(0, name.length + 1) == (name + '=')) {
                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                break;
            }
        }
    }
    return cookieValue;
}

$.ajaxSetup({
    beforeSend: function(xhr, settings) {
        if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
            // Only send the token to relative URLs i.e. locally.
            xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
        }
    }
});


$(document).ready(function() {
    $("#email").focusout(function(e) {
        e.preventDefault();
        var csrftoken = getCookie('csrftoken');
        var email = $('#email').val();

        $.ajax({
            url: "/email_check/",
            type: "POST",            
            dataType: "json",
            contentType: "application/json",
            data : { 
                csrfmiddlewaretoken: csrftoken, 
                email: email
            },
            success: function(result) {
                console.log(result);
            },
        });
    });
});
</script>

{%csrf_令牌%}
电子邮件
函数getCookie(名称){
var-cookieValue=null;
if(document.cookie&&document.cookie!=''){
var cookies=document.cookie.split(“;”);
对于(变量i=0;i
我可能跳过了一些步骤,你能帮我理解它们是什么吗


谢谢

$.ajax()中删除以下参数

它会起作用的


我在本地对它进行了测试,当内容类型为
application/json
时,它似乎无法按预期工作。对于POST请求,django可能希望内容类型为
application/x-www-form-urlencoded

我不确定这是问题的原因,但您的
输入
有一个错误的
标记和无标签标记感谢您的指出,但不幸的是,这似乎不是问题所在!
dataType: "json",
contentType: "application/json",