Leveraging JavaScript to employ AJAX for communicating with a Spring controller

I'm having trouble accessing the controller's method

var url = "UsersGroupReader";
if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
}
req.onreadystatechange = processAccessGroupRequest;
req.open("GET", url, true); 
req.send(null);

The controller code is:

@RequestMapping(value = "UsersGroupReader", method = RequestMethod.GET)
public Vector<String> readUsersGroup(HttpServletRequest request,
        HttpSession httpSession) {

The response I am getting is a 404 error when using AJAX.

The mapping in my web.xml looks like this:

<servlet-mapping>
    <servlet-name>login</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

I have tried using UsersGroupReader.html in both the AJAX call and the controller, but still can't access it.

Can someone help me find the mistake?
This issue might be similar to this one, but unfortunately following those steps didn't fix it.

Thank you in advance!

Answer №1

If the request you are sending isn't reaching its intended server, it's possible that the DispatcherServlet is not being triggered. To diagnose such problems, you can often uncover the root cause by:

  1. Inspecting the exact URL being sent in the request using your browser's developer toolbar.
  2. Increasing Spring's logging level to debug mode to understand its actions upon receiving the request (if it receives it).
  3. Adjusting the servlet-mapping configuration accordingly.

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

I am encountering an issue with Angular where the following error message is displayed: "src/app/app.component.html:18:20 - error TS2339: Property 'DepScreen' does not exist on type 'AppComponent'"

This code snippet is from my app.component.html file: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0" ...

Angular-nvd3: maintaining consistent spacing between data points along the x-axis

By default, the scale of the x-axis is calculated from values, resulting in uneven distances between two adjacent points. For example, with an array of values like [1,2,5], there will be different distances on the x-axis for each point, and the x-axis labe ...

Guide to making a basic cross-domain ajax request that fetches an HTML webpage

I have been researching various methods for making cross domain calls using ajax, but they all seem overly complex or too specific. Below is a basic HTML page where I am trying to send request parameters to a specific JSP on my server. <!DOCTYPE html&g ...

When transmitting a variable from JavaScript to PHP using Ajax, the data appears to be missing

While attempting to pass a simple string variable using an onclick event, I encountered an issue where the request was successful but the response in the console displayed as empty. Additionally, the xvar variable did not come through properly resulting in ...

Passing Down Instance Methods Using Static References in JavaScript/TypeScript

✋ Exploring the concept of access modifiers from TypeScript, how can we make it relevant for JavaScript developers as well? Let's consider a scenario where a parent class defines necessary members and a shared method: // ParentClass.js export defaul ...

Using AJAX to call a PHP function within a PHP script

I have successfully implemented an AJAX file that can execute content from a PHP file. However, my concern is how to specifically call a particular function within the PHP file if there are multiple functions present. Below is my current AJAX code: $(doc ...

What is Causing The Card to Not Expand?

I wanted to enhance my project by incorporating a responsive card template, so I turned to one of the templates on CodePen for help. However, after copying the code into my own platform, I encountered an issue where the card wouldn't expand when click ...

What is the process for creating a transparent default color theme in MUI?

I'm looking to make an element's background color the primary main color with some transparency using Material-UI. I've attempted a couple of methods, but unfortunately neither have worked for me. Any suggestions or guidance would be greatly ...

Bringing in PeerJs to the NextJs framework

Currently delving into NextJs and working on creating an audio chat application, I've hit a roadblock while attempting to import PeerJs. An error message keeps popping up saying 'window is not defined'. import Peer from 'peerjs'; ...

Guide to verifying a username input using Java and MySQL to prevent the occurrence of duplicate usernames

Greetings, I am encountering an issue with the method below. It is throwing an error: "Java.sql.SqlException: statement is not executing." The problem seems to occur when checking for duplicate values, as indicated by the code snippet. This issue is prev ...

Display a pop-up alert following the completion of form validation using JavaScript

I am currently learning JavaScript and working on an enrollment form that requires validating a cellphone number with 11 characters. The issue I'm facing is that every time I hit submit, the page refreshes instead of displaying the validation message. ...

How to retrieve the present value from an array using AngularJS

Struggling to assign the current user from my list Here is my array after submitting the form [{"name":"Erich","surname":"Josh","email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="096c67497a7a276a6664">[email prot ...

Adding a Row to an HTML Table Inside a Form through JavaScript

I'm currently attempting to insert a row into a table that resides within a form using JavaScript. The strange thing is, the code functions perfectly when it's placed outside of the form tags. However, as soon as I enclose the code within the for ...

Discovering deeply nested data within MongoDB documents using Mongoose

Is there a way to retrieve all tasks based on the first name of the claimant? Task Schema: const TaskSchema = mongoose.Schema({ case: { type: mongoose.Schema.Types.ObjectId, ref: "Case" }, }); Case Schema: const CaseSche ...

AbstractTransactionalJUnit4SpringContextTests in Spring Profiles

Our integration tests are crucial for verifying various aspects of our system, such as the state of the database in different environments (Dev, Test, Qa, etc) in relation to our JPA mappings and other data-dependent elements. We specify the database and ...

differences between object access and array access in JavaScript

I have been accumulating a series of data that increases gradually in size. Now, I am faced with the task of finding a specific row within this data using its unique Id. In my quest to optimize this search process, I have considered two potential options. ...

Is it possible to retrieve JSON data from the server without having to reload the page?

My approach to minimize server requests involves sending a JSON file to the client and working with that data instead. However, a challenge I'm facing is preventing the page from refreshing when receiving the JSON file. I want the data to be received ...

Comparing arrays of objects to filter against each other?

customerItems: [ { label: "itemA", serialNumber: 123 }, { label: "testItem", serialNumber: 44 } ] otherItems: [ { label: "anotherItem", serialNumber: 44 }, { label: "testItem&quo ...

Sending data using jQuery when a button is clicked

Is there a way to display the text from a text box when a button is clicked? Here's what I have been attempting: <script type="text/javascript> $(function() { $('#button').click(function(){ $.ajax({ type: "PO ...

Adjusting the mouse movement update frequency – what you need to know!

As I work on developing a function that requires a quick and easy signature, I am facing an issue with the canvas field. Despite using jQuery to handle the signature, the refresh rate of mousemove coordinates seems too slow. This results in white spaces be ...