Skip to content
Advertisement

parse json object in django view

I have a form than i need to check if the emails on my form are valid or not here is my ajax code :

<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)
        }

and this is my code in django view:

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

    return JsonResponse(status, safe=False)

it raises this error : TypeError: the JSON object must be str, bytes or bytearray, not ‘NoneType’ thanks in advance

Advertisement

Answer

TypeError: the JSON object must be str, bytes or bytearray, not ‘NoneType’

As the error says, the object that json.loads is trying to parse is somehow None, you would need figure out why the object is null, set a breakpoint in the validate function or print the value of request.POST to check what data is available on the server

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement