One of the json.dumps outputs contains a null value

I am currently configuring a front-end user authentication check using Parsley.js and Django. Here is my view:

@requires_csrf_token
def password_check(request):
    email = request.POST.get('email')
    password = request.POST.get('password1')
    user = authenticate(email=email, password=password)

    if request.is_ajax():
        if user and user.is_active:
            res = "These password and e-mail are ok."
            ajax_vars_login = {'response': res, 'email': email, 'password': password}
            json_data_login = json.dumps(ajax_vars_login)
        else:
            res = "The e-mail or the password field aren't correct."
            ajax_vars_login = {'response': res, 'email': email, 'password': password}
            json_data_login = json.dumps(ajax_vars_login)

        return HttpResponse(json_data_login, content_type='application/json')

And here is the Parsley validator being used:

Parsley.addAsyncValidator(
  'emailPwCombination', function (xhr) {
       var password = $('#password').parsley();
       var email = $('#email').parsley();
       var response = xhr.responseText;
       var jsonResponse = JSON.parse(response);
       var jsonResponseText = jsonResponse["response"];

       if(jsonResponseText == 'These password and e-mail are ok.')
           return true;
       if(jsonResponseText == '404')
           return false;
  }, '/password_check/'
);

The issue I am facing is that it seems like the email is not being sent to the server because I receive two XHR responses each time I submit. The first response is:

{password: null, response: "The e-mail or the password field aren't correct.",…}
email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="97f2eff6fae7fbf2d7f0faf6fefbb9f4f8fa">[email protected]</a>"
password: null
response: "The e-mail or the password field aren't correct."

And the second response is:

{password: "examplepassword", response: "The e-mail or the password field aren't correct.", email: null}
email: null
password: "examplepassword"
response: "The e-mail or the password field aren't correct."

I'm trying to figure out what I might be missing. Any insights?

Answer №1

When you use this function within an async validator, it triggers a separate request to the backend every time .parsley() is called on a specific value. This means that if you are checking multiple values, there will be multiple requests sent - one for each individual field.

It seems like there may be some confusion about the purpose of parsley in this context. Its main function is to validate individual fields based on certain criteria, rather than sending all form data to the backend for processing. Achieving this can actually be quite straightforward using basic jQuery without relying on parsley at all.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Dynamic stacking of buttons in response to user navigation choices

Is it possible to create a navigation bar with 5 buttons using CSS and JS? Here is what I am looking to achieve: Display 5 nav/button options When a user clicks on a button, the other buttons should transition or hide, leaving only the selected button vi ...

JEST - Highlighting the presence of asynchronous methods with yellow lines on the coverage report

Experiencing an issue on Vue JS, specifically with a method within a component: https://i.sstatic.net/yAjr0.png The method loadProducts is async, and running JEST tests reveals that line 320 is not covered, despite all other lines and code within the func ...

I am facing an issue with the javamail.setFrom() method in my Spring application

When using Java mail, the user is trying to implement the functionality of receiving inquiries via email. However, there seems to be an issue with the setFrom() function where the authenticated mail address overrides the email address that is being set by ...

Filtering for Material Autocomplete is limited to the getOptionLabel field

Currently, I am utilizing the Google Material-UI autocomplete component. It is currently only filtering based on the "getOptionLabel" option field when text is entered into the input field. However, I would like the autocomplete to filter based on more tha ...

Can you provide some insight on how to store XMLHttpRequest response in Javascript for future

I have a function in my codebase that is responsible for loading HTML templates asynchronously: loadTemplate: function(url) { return new Promise(function(resolve, reject) { var xhr = new XMLHttpRequest(); xhr.open("GET" ...

Is memory leakage a reoccurring issue every time a function is executed in JavaScript?

Currently I am taking a JavaScript course where the instructor mentioned that every function you create acts as a constructor and each time it is called, a new object is generated. Let's look at the following code example: function Test(){ Console ...

Highcharts: Show tooltip above all elements

Is there a way to configure tooltip display above all elements? I want to define specific coordinates so that the tooltip remains open even if it is covering the chart, and only closes when interacting with the top block. Screenshot 1 Screenshot 2 For e ...

Implementing personalized validation in an AngularJS form

Looking at the form below, I am currently ensuring that an email address is required: http://jsfiddle.net/U3pVM/16994/ My goal now is to enhance the validation process by checking that the first two characters of the email address begin with 'DD&apo ...

Synchronizing Form Data in Angular 5: Pass and Populate Dropdowns between Components

I have developed a unique form (material dialog modal) that allows users to create an account. When the user clicks on the Register button, their created account should appear in a dropdown menu without redirecting or reloading the current page. I am facin ...

Utilizing Vue-i18n for language translations directly within a JavaScript file, rather than using

Is there a way to utilize the .js file for translations instead of the .json file? I attempted changing: const locales = require.context('./locales', true, /[A-Za-z0-9-_,\s]+\.json$/i) to const locales = require.context('./loca ...

Can you tell me where the entry point of a Django application is located, similar to main.py? And where should I configure the parameters for

I'm looking to establish certain parameters and initialize the Injector object for my Django app so that I can incorporate dependency injection and singletons. Typically, I handle this in the main.py of my application, but with Django, I'm unsure ...

Transforming a string containing numerical arrays into their corresponding values

I'm currently tackling a coding task that involves receiving a text input from the user in a specific format like shown below: value = "[1, 2, 3, 4]\n[9, 8, 7, 6]\n[1, 2, 3, 4]"; When using .split, the value is transformed into an array ...

The tooltip in nvd3 is displaying the index instead of the label

I'm encountering an NVD3 tooltip issue with my multichart (multiline chart). The XAxis labels are set as JAN, FEB, MAR... DEC. However, when I hover over the graph, it displays 0, 1, 2, 3... 11 as the tooltip title instead of the month names. Here is ...

Unraveling the HTTP response in Angular - Troubles with JSON parsing

After spending a considerable amount of time, I have reached the stage where I am able to receive the following body in a 404 error response from my server. However, I am encountering difficulty in parsing the content into Angular in order to use it. I rea ...

Secure Access: Passport.js User Verification & Authorization

Recently, I successfully developed my first User login and authentication system using Passport.js. While the system works as intended, I am struggling to fully comprehend the code itself despite reading numerous articles and examples online. I would great ...

Decoding JSON in Objective C with integer key names incremented consecutively

I'm having some trouble figuring out how to parse this JSON data. It's structured in a way that I'm not quite used to: "nodes": { "4": { "node_id": 4, "title": "TITLE 1", "description": "", }, "7": { ...

Accept a JSON-sending POST request in a JSP file via JavaScript

I have developed an HTML page with an input field. Using javascript, I extract the input value, convert it into JSON format, and attempt to send it through ajax. Additionally, I have a JSP application where a Java method processes this JSON data to store i ...

What is the best way to emphasize a table column when a cell is selected?

I successfully implemented a feature where a cell (td-element) is highlighted when clicked by the user. However, I now want the entire column to be highlighted with the cell itself having a slight variation. I am struggling with achieving the last part ...

What could be the reason why only my drawLine function is functioning correctly?

As I embark on learning canvas and its control, I've encountered an issue with my code. The drawLine function, which should draw a dot or line on the canvas when triggered by onMove, is not rendering anything on the canvas surface. Can someone please ...

Line numbers in Vue Codemirror

Currently, I'm working on integrating codemirror into my Vue.js application using a library found at https://github.com/surmon-china/vue-codemirror. The issue I'm facing is that even after importing and utilizing the codemirro component, everythi ...