【问题标题】:parse json object in django view在 django 视图中解析 json 对象
【发布时间】:2020-11-04 20:25:58
【问题描述】:

我有一个表格,我需要检查表格上的电子邮件是否有效这里是我的 ajax 代码:

<script type="text/javascript">
    $(document).ready(function (event) {
        function ValidateEmail(mail) {
            if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(mail)) {
                var postEmail = $("div.postEmail");
                
                if (!$("#members_email").val() == "" || !$("#members_email").val() == null) {
                    $.post(
                        "{%url 'validate_email' %}",
                        {
                            email: $("#members_email").val()
                        },
                        function (data, status) {
                            console.log("Data: " + JSON.stringify(data) + " " + status);
                           
                          
                            postEmail.append(JSON.stringify(data));
                            
                            
                            // contaner.append("Data: " + JSON.stringify(data));
                        }
                    );
                };
                console.log('email is true');
                return (true);

            }
            console.log('email is false');
            return (false)
        }

这是我在 django 视图中的代码:

def validate_email(request):
    data = json.loads((request.POST.get('postEmail')))
    status=True

    return JsonResponse(status, safe=False)

它引发了这个错误: TypeError:JSON 对象必须是 str、bytes 或 bytearray,而不是 'NoneType' 提前谢谢

【问题讨论】:

    标签: python django


    【解决方案1】:

    TypeError:JSON 对象必须是 str、bytes 或 bytearray,而不是 'NoneType'

    正如错误所说,json.loads 试图解析的对象不知何故为 None,您需要弄清楚为什么该对象为 null,在 validate 函数中设置断点或打印 request.POST 的值以检查服务器上有哪些可用数据

    【讨论】:

      猜你喜欢
      • 2013-06-16
      • 2017-01-10
      • 1970-01-01
      • 2020-09-03
      • 2021-03-19
      • 2014-11-22
      • 2020-05-08
      • 1970-01-01
      • 2016-11-13
      相关资源
      最近更新 更多