Troubleshooting Django user login Ajax problem with Bootstrap 3.x modal

After submitting the modal form, an AJAX request is made to pass the username and password to a view and retrieve JSON information to update the form. I have confirmed that the JsonResponse(rsdic) has been executed on the server side and the client receives the JSON info successfully through the AJAX success function.

However, I am facing difficulty in updating the #error_message1 field embedded in the model. I have tried using .html() or .append(), but both methods have failed.

I have provided the key portions of the code for your reference. Thank you again for your assistance.

AJAX Component:

$.ajax(
    {
         type: "POST",
         url: url,
         data: {
              'username': username,
              'password': password
         },
         dataType: 'json',
         success: function(data){
              $('#login-form')[0].reset();
                  if (data.ret != '1107'){
                    var htmlcode ="<p> data.info </p>";
                    $('#modal').find('.error_message1').append(htmlcode);
                }
            },
                error: function (data) {
                    console.log('An error occurred.');
                    console.log(data);
                 },
     });

HTML Component:

<div class="modal fade" id="modal">
    <div class="modal-dialog" role="document">  
        <div class="modal-content">
            <div id="form-modal-body" class="modal-body">  
                  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times</button>
                  <h4>EXP LOGIN</h4>
                <form role="form" action="{% url 'auth_login' %}" method="post" id="login-form">
                    {% csrf_token %}
                    <ul>
                       <li>
                         <div class="g-icon"><i class="iconfont icon-yonghu"></i></div>
                         <input type="text" name="username" id="username" value="" placeholder="User name" />
                       </li>
                      <li>
                         <div class="g-icon"><i class="iconfont icon-mima"></i></div>
                         <input type="password" name="password" id="password" value="" placeholder="Password" onblur="check1()" />
                         <div class="g-cue" id="error_message1">Your email or password was entered incorrectly.</div>
                      </li>
                    </ul>
                    <div class="g-btn">
                      <input class="g-submit" id='login-button' type="submit" value="{% trans 'Log in' %}" />
                      <input type="hidden" name="next" value="{{ next }}" />
                    </div>
                    <p><span>{% trans "Not a member?" %} <a href="{% url 'registration_register' %}">Join now.</a></span></p>

               </form>
            </div>  
         </div>
      </div>
</div> 

  

Answer №1

There is an issue with using error_message1 as an id, rather than a class

$('#modal').find('#error_message1').html(htmlcode);

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

Setting up a Node.js http2 server along with an http server specifically designed for a single

In my upcoming project, I am interested in implementing the http2 protocol. My goal is to have both http and http2 servers running on a single domain and port, if feasible. When a client that does not support http2 connects, communication will default to ...

Node.js (Express), passport.js, mongoose.js, and Android app all have in common the HTTP Error 307 - Temporary redirect

I am currently constructing a REST Api using Node.js (Express.js and Moongose.js). I have a single post route that takes JSON data and redirects it to the signup page (/app/signup) if the user is a first-time user (not found in the database), or to the log ...

Preventing file visibility in Three.js resource directory

Once a user clicks on a specific 3D model, I retrieve it from the server and render it in the browser using three.js. However, there is an issue when the user tries to access a model that is not free - they can easily view and download the STL file by go ...

Implementing a search filter for special characters in AngularJS

Looking to filter an array of players by their names, but facing a challenge with special characters in the names. Found a code snippet on Google that looks like this: $scope.modelFilterNormalized = function(){ if($scope.modelFilter) return $sco ...

Is it acceptable to include multiple modules within a single JavaScript file?

One thing that I've noticed is that the AngularJS (*.js) files I've come across typically only have one module, and the module name doesn't always match the file name. I'm curious to know if it's possible to include multiple modul ...

Getting the value of a button using JavaScript

Is there a way to retrieve the value of a button when multiple buttons are generated dynamically? I have a JavaScript function that creates buttons in a list based on my search history, with each button labeled as a city name. However, after clicking on o ...

Is there a way to transform a SQL BIT into JSON using jQuery and then showcase it in an HTML format?

Currently, I am dealing with the interaction of checkboxes and JSON data. My main goal is to find an efficient and clean method to set a checkbox based on JSON data retrieved from a BIT field in SQL. The code snippet below achieves this, but I feel like th ...

Looking for assistance with customizing my jQuery input styling

Check out this simplified version of the code: $(document).ready(function() { var $checkbox = $('input[type="checkbox"]'); $checkbox.wrap('<div class="stylecheck-container" style="display: inline" />').addClass('style ...

Exploring the intricate design and information dynamics of MRAID 2

I am currently in the process of developing a new MRAID (v2) compliant SDK for Android that will enable rich media ads to be displayed within various android apps. Additionally, I plan to implement a backend platform that allows advertisers to create their ...

What is the process for cancelling an interval when it is disabled in my configuration file?

To automate a bot, I want it to stop running an interval if the configuration file specifies "off" and continue running if it says "on". I attempted this: Using discord.js: config.Interval = setInterval(() => { WallCheck.send(WallCheckemb ...

What is the best way to show a message of success once the user has been redirected to the homepage?

Currently, I have a registration form utilizing AJAX and PHP for validation. Error messages can be displayed on the registration page if the user does not correctly fill out the form. Upon successful registration, the user is redirected back to the home pa ...

Initiate a function once the innerHTML content in Angular has been completely loaded

I'm curious to know if it's possible in Angular to receive a notification when the Inner HTML has finished loading. I have a web application that retrieves an SVG image from a server and I need to display it as innerHTML in order to interact with ...

The state may be modified, but the component remains unchanged

I've been tasked with implementing a feature on a specific website. The website has a function for asynchronously retrieving data (tickets), and I need to add a sorting option. The sorting process occurs on the server side, and when a user clicks a s ...

css based on the current time in the United States

I have a working code that currently reads the user's computer time, but I specifically need to get the hours from the USA regardless of the user's location. The CSS should be applied based on USA time. <script type="text/javascript"> dat ...

The Material UI autocomplete unexpectedly closes on click instead of displaying a popover as intended

I'm facing a challenge with adding a button to the Material UI autocomplete using the renderOption prop. I added a button to each row that was intended to display a PopOver component, but when clicking on the button, it automatically closes the autoco ...

Tips for successfully executing child_process.exec within an ajax request

On a server that I have access to but not ownership of, there is a node js / express application running on port 3000. Various scripts are typically executed manually from the terminal or via cron job. My goal is to have a button on the client-side that tr ...

Submitting values using the serialize method and Ajax involves sending placeholders

Looking for a solution: <form class="pure-form pure-form-stacked" method="post" enctype="multipart/form-data"> <input type="text" name="name" class="button-size-1" placeholder="*Name"> <input type="text" name="email" class="but ...

Retrieving the user's Windows username with JavaScript

Is it possible to retrieve the Windows user name solely in Internet Explorer using the code below? function GetUserName() { var wshell = new ActiveXObject("WScript.Shell"); alert(wshell.ExpandEnvironmentStrings("%USERNAME%")); } What methods ...

When the FileReader reads the file as readAsArrayBuffer, it ensures that the correct encoding is used

Currently, I am developing a script in JavaScript to read uploaded .csv/.xlsx files and convert the data into an array containing each row. Using FileReader along with SheetJs, I have successfully managed to achieve this by implementing the following code: ...

Tips for determining whether the current request is an AJAX request in MVC3 view using JavaScript

I have a div with a maximum height and overflow set to auto. When the size of the div overflows, a scroll bar is automatically added. However, I have also set $("#itemsdiv").scrollTop(10000); so that the scroll bar is always at the bottom. Now, I want t ...