What could be causing the issue with updating a js file using ajax?

I've been dealing with a php file called users. Initially, everything was going smoothly as I wrote some JavaScript code for it. However, after making updates to the JavaScript code, it seems to have stopped functioning.

Below is the content of the php file (users.php):

 <div class="wrapper">
  <section class="users">
    <header>
      <?php 
        include_once("php/config.php");
        $sql = mysqli_query($conn,"SELECT * FROM users WHERE unique_id={$_SESSION['unique_id']}");
        if( mysqli_num_rows($sql) > 0 ){
          $row = mysqli_fetch_assoc($sql);
        }          
      ?>
      <div class="content">
        <img src="php/images/<?php echo $row['img'] ?>" alt="" />
        <div class="details">
          <span><?php echo $row['fname']." ".$row['lname'] ?></span>
          <p><?php echo $row['status'] ?></p>
        </div>
      </div>
      <a href="#" class="logout">Logout</a>
    </header>
    <div class="search">
      <span class="text">Select an user to start chat</span>
      <input type="text" placeholder="Enter name to search..." />
      <button><i class="fas fa-search"></i></button>
    </div>
    <div class="users-list">         
     
    </div>
  </section>
</div>
<script src="javascript/users.js"></script>

Here is the JavaScript file content (users.js):

          //1st part
          const searchBar = document.querySelector(".users .search input"),
         searchBtn = document.querySelector(".users .search button");

         searchBtn.onclick = () => {
            searchBar.classList.toggle("active");
            searchBar.focus();
             searchBtn.classList.toggle("active");
          };    


         //2nd part
         setInterval(() => {
           console.log("habib");
              let xhr = new XMLHttpRequest(); 
              xhr.open("GET", "php/users.php", true);
              xhr.onload = () => {
                 if (xhr.readyState === XMLHttpRequest.DONE) {
                    if (xhr.status === 200) {
                        let data = xhr.response;
                         console.log(data);
                     }
                   }
              };

             xhr.send();
       }, 500);

Initially, the first part of the JavaScript program worked without any issues. However, after adding the second part, things broke down. Strangely, restarting my PC resolved the problem. But I'm curious to understand why this occurs?

The console displays the following error message.. https://i.stack.imgur.com/fjwl7.png

Answer №1

Perhaps the solution lies in addressing a browser issue, particularly if you are using Chrome. Chrome tends to cache JS and CSS files heavily for quicker page loading, however, this can be problematic for web developers. To resolve this dilemma, consider the following steps:

  1. Force a page refresh by pressing Ctrl + F5
  2. Rename the file so that Chrome will reload the page
  3. Switch to lighter browsers like Edge or Firefox

To test this theory, open your JS and CSS files in the browser, make changes, and attempt to reload the code; if you are using Chrome, you may notice that the code remains unchanged unless one of the solutions above is applied.

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

"An error message stating 'Express: The body is not defined when

I'm encountering an issue while trying to extract data from a post request using express. Despite creating the request in Postman, the req.body appears empty (console.log displays 'req {}'). I have attempted various solutions and consulted s ...

Mastering the art of debugging a mongoose action in node.js

I am utilizing mongoose for connecting my node.js app with mongoDB. However, I am facing an issue where the database does not get updated when I create or update a model instance. How can I effectively debug and identify what goes wrong in the create or up ...

obtain information from a Java servlet on a web page

I am working on a webpage that displays various coordinates (longitude, latitude), and I need to create a Java class to sort these coordinates. My plan is to send the coordinates to a Java servlet before displaying them on the webpage. The servlet will th ...

The appearance of an unforeseen * symbol caused a

Having issues with this particular line of code, import * as posenet from '@tensorflow-models/posenet' The error 'Uncaught SyntaxError: Unexpected token *' keeps popping up, I have the latest version of Chrome installed and I've ...

What is the process to ensure a React JS click handler updates the DOM upon execution?

I have almost completed a demo app using React JS, where data is displayed in a table. In the status column, hovering over an item triggers a popup div with 3 options to click on. After clicking on an option, I am able to run the click handler, but I' ...

Adding Bootstrap component via ajax request

I'm facing an issue with injecting a Bootstrap component using ajax. I usually include a select element like this: <select class="selectpicker" data-width="75%"> Most of the HTML code is generated dynamically through javascript, which you can ...

Updating data in AngularJS after inserting a new record

What is the most efficient method to update comments data when a new record is added to the database? I currently have this code that works well, but I am concerned that it may be slow if there are a large number of comments. Any assistance would be greatl ...

A step-by-step guide to creating a clipboard stream using guacamole-common.js

When utilizing Guacamole.client.createClipboardStream to generate a clipboard stream and sending the stream on paste event, I noticed that the remote clipboard remains empty. const customStream = client.createClipboardStream("text/plain"); cust ...

What is preventing me from accessing my session array in this.state.props from my mapStateToProps in React-Native Redux?

I am currently facing an issue with my Redux store setup. I am attempting to store an array of Session objects, where each Session object contains an array of Hand objects. However, when trying to access my store using `mapStateToProps`, none of the option ...

saving information into a MySQL database

I am facing an issue where I am trying to write data to MySQL. When I input the data and press the submit button, the console log message from the function indicates that everything is okay. However, upon checking the database, there is nothing saved. Can ...

Is it possible to move a directive within a limited parent element?

Is there a way to limit the movement of an angular.js drag directive within the confines of its parent element? You can see the problem I'm facing in this jsbin: http://jsbin.com/maxife/3/edit?html,css,js,output ...

Having difficulty modifying the styling of a paragraph within a div container

I have been working on a function that is supposed to adjust the font-size and text-align properties of a paragraph located within a div tag once a button is pressed. function customizeText() { document.getElementById('centretext').innerHTML = ...

Uploading Files in Django Using Ajax Without a Form

As I explore using Valum's Ajax Upload for file uploads on my Django-based website, I am faced with a challenge. Currently, I am avoiding a traditional form because AU sends the entire upload as POST data in an AJAX request. My current approach involv ...

How to retrieve a variable from an object within an array using AngularJS code

I recently started learning TypeScript and AngularJS, and I've created a new class like the following: [*.ts] export class Test{ test: string; constructor(foo: string){ this.test = foo; } } Now, I want to create multiple in ...

Having trouble retrieving the JSON data received from the backend

After making an AJAX request, I receive a response which is then saved to a data variable. This is the controller logic: def retrieve data = params[:data] @question = Question.find_by(id: params[:question_id]) @choices = @question.choices results ...

Substitute the content within an <li> tag

Is there a way to replace or entirely remove - undefined without using substr()? The value of 'name' is dynamic and constantly changing. Any suggestions would be greatly appreciated. <li>name - undefined</li> <li>name - undefin ...

The jQuery script removal functionality originated from the use of ajax

I have adapted a nested modal script (jQueryModal) to better suit the project's requirements, but I've encountered an unusual issue that I can't seem to resolve. When I invoke the modal to load some content via ajax, and the response from t ...

Insert HTML content into an iframe with a callback function

We are receiving information from the backend server and need to transfer it to an iframe. In order to accurately set the height of the iframe to match the content, we must wait for the content to be loaded into the iframe. However, this process may not ha ...

What methods can I use in Mocha with Sinon for testing multiple callbacks?

My code involves a function with asynchronous operations and multiple callbacks var f = (cb1, cb2) => { return new Promise((resolve, reject) => { /* ... */ }); }; During testing, I wanted to use sinon to create spies var cb1Spy = sinon.spy(); va ...

What is the appropriate method for passing parameters in PHP and how can you handle returned empty values

I'm looking to pass parameters in the action method because I am unable to do so via the header. <form name="mailinglist1" method="post" action="report1.php" > In this form, I'm using a download button to connect my report (HTML). ...