【问题标题】:Existing E-mail check (Django + Javascript/Ajax)现有的电子邮件检查(Django + Javascript/Ajax)
【发布时间】:2016-06-28 16:27:47
【问题描述】:

我一直在尝试使用 Django 和 Javascript/Ajax 构建电子邮件验证器,但我遇到了困难。我得到了 Ajax 响应

{response: "This field is required.", email: false}
email: false
response: "This field is required."

但它总是一样的,即使我填写了电子邮件字段。

这是我的 view.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>

我可能跳过了一些步骤,你能帮我理解它们是什么吗?

谢谢!

【问题讨论】:

  • 我不确定问题的原因,但您的input 有一个错误的&lt;/a&gt; 标签并且没有标签标签
  • 感谢您的指出,但不幸的是,这似乎不是问题!

标签: javascript jquery json ajax django


【解决方案1】:

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

dataType: "json",
contentType: "application/json",

它会起作用的。

我在本地对其进行了测试,当内容类型为 application/json 时,它似乎没有按预期工作。也许 django 期望 POST 请求的内容类型为 application/x-www-form-urlencoded。

【讨论】:

    猜你喜欢
    • 2018-04-15
    • 2011-04-16
    • 1970-01-01
    • 2017-12-20
    • 1970-01-01
    • 2018-09-09
    • 2011-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多