Unintended redirects are occurring with AJAX requests when using the send() method

After loading the web page, I utilize AJAX to populate a list asynchronously. However, when the AJAX request is sent to retrieve data for the list box, instead of receiving JSON data as expected, an unintended web page containing the list box is returned.

[Components]
haveList.jsp: This file contains the list box and includes the <script src="loadData.js"> tag.
loadData.js: It generates an AJAX request to the server right after haveList.jsp is loaded. (I used an alert to confirm that the request retrieves the correct data from the server.)
returnPage.java: A servlet that forwards to haveList.jsp
returnJsonData.java: A servlet that returns the JSON string requested by loadData.js
These components are detailed below.

[web.xml]

<servlet>
<servlet-name>ReturnPage</servlet-name>
<servlet-class>pkg.ReturnPage</servlet-class>    
</servlet>
<servlet-mapping>
<servlet-name>ReturnPage</servlet-name>
<url-pattern>/openHaveList.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>DataManager</servlet-name>
<servlet-class>pkg.ReturnJsonData</servlet-class>    
</servlet>
<servlet-mapping>
<servlet-name>DataManager</servlet-name>
<url-pattern>/requestListData.do</url-pattern>
</servlet-mapping>

[Phenomenon]
When I sent "openHaveList.do" to the server, it returned the page "haveList". Immediately afterward, an AJAX request was made to fetch JSON data (requestListData.do) as specified in loadData.js. Upon inspecting the responseText using alert(), I noticed that instead of JSON data, the HTML content of haveList.jsp was returned.

[Clues]
(1) When unintentional HTML content was received from the server, the browser's address bar displayed http://localhost/openHaveList.do. (This occurred when clicking on an image on another page.)
(2) By examining the request/response with ZAP (Zed Attack Proxy), I observed that 2 requests were made: - The first: requestListData.do (Response: empty) - The second: openHaveList.do (Response: HTML content of haveList.jsp) Due to this observation, I suspect that the AJAX request is being redirected unintentionally (from requestListData.do to openHaveList.do).

- Source Codes -
[haveList.jsp]
Contains nothing special. Just a list box.

[loadData.js]

(function () {
var conn=null, addEvent=null, createXHR=null, loadNations = null, setAsyncLoad=null;
// More JavaScript code here...
}()); 

[returnPage.java]

// Java code for ReturnPage servlet.

[returnJsonData.java]

// Java code for ReturnJsonData servlet.

Answer №1

Hooray! I've successfully resolved the issue!

[Explanation]
The successful resolution was attributed to my Filter.

[Elaboration]
In reality, haveList.jsp functions as a login page. To ensure security, I have a filter in place that verifies if the request is logged in. If not, the filter redirects the request to "haveList.jsp (login page)".
When making an ajax call to the server ( requestListData.do ), the filter redirected the request to the login page due to lack of authentication. Please refer to the earlier version of web.xml below.

<filter>
<filter-name>Checker</filter-name>
<filter-class>filter.Checker</filter-class>
<init-param>
<param-name>escape</param-name>
<param-value>/Test/,/Test/gate.jsp,/Test/openHaveList.do</param-value>
</init-param>   
</filter>
<filter-mapping>
<filter-name>Checker</filter-name>
<url-pattern>*.do</url-pattern> 
</filter-mapping>
<filter-mapping>
<filter-name>Checker</filter-name>
<url-pattern>*.jsp</url-pattern>    
</filter-mapping>  

[Resolution]
To address this issue, I included the ajax call to the server as an exception (defined in the escape parameter). In my filter servlet, I modified it to not programmatically redirect such exceptions to the login page. The updated version of web.xml is shown below.

<filter>
<filter-name>Checker</filter-name>
. . .
<param-value>/Test/,/Test/gate.jsp,/Test/openHaveList.do,"/requestListData.do"</param-value>
. . .

Following these changes, I successfully received JSON data from the server. Victory at last! 😊

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

Auth0 and Vue.js work together seamlessly to easily redirect users back to their previous page

I have integrated auth0 into my Vue.js application for handling logins. Within my AuthService, I manage all the auth0 operations. In the constructor, I instantiate a new auth0 object and set the default redirectUrl to the desired location: import auth0 f ...

Adding Gridster to a WordPress theme

I am having an issue with implementing Gridster into my WordPress plugin. Despite correctly loading the necessary files from the folder, it does not seem to work. function add_my_stylesheet() { wp_enqueue_style( 'myCSS', plugins_url( ' ...

What is the speed of communication for Web Worker messages?

One thing I've been pondering is whether transmission to or from a web worker could potentially create a bottleneck. Should we send messages every time an event is triggered, or should we be mindful and try to minimize communication between the two? ...

Pass the ID parameter to the AJAX jQuery function

Hi everyone! I have a search input field where the value is sent to an AJAX jQuery function successfully, and the result is displayed in a div. Now, I want to know if it's possible to send the ID value from that div to another AJAX jQuery function. I& ...

How to store and retrieve images using the Google Drive API in a Node.js application

I am new to working with Node.js. I have been exploring how to route an incoming image from the Google Drive API without having to download it first, as that process takes too long. My goal is to send the image directly to the client once I receive it. I ...

What is preventing me from making an inline call to res.json?

In my expressjs application, I encountered an issue with inlining a callback when using the res.json method to respond with a database document. While inlined calls to console.log worked fine, the program failed when I attempted the same approach with res. ...

What is the best way to send data from ajax to a django view?

I have a script for ajax that retrieves the public key and sends other details to the Stripe server for payment. However, I am struggling with sending the dynamic value of the price to item_line. fetch("/config/") .then((result) => { return re ...

The router should display a component based on the user's access level, even if they are

I'm working on a scenario where the home route needs to cater to both admin and user roles. Is there a way to dynamically display the UserComponent if logged in as a user, and show the AdminComponent if logged in as an admin? This is my current setup ...

Capturing page titles accurately for timeonsite tracker in a single-page Angular app is challenging when navigating to other pages

Implemented the timeonsite JS tracker in my Angular web application using HTML tags as shown below, <script type="text/javascript"> var Tos; (function(d, s, id, file) { var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementByI ...

Displaying country-specific API details (such as capital and currency) in a card container when selecting a country from a dropdown menu

My objective is to display the card information for South Africa as the default value, even before entering any country's name into the search bar input field or selecting a specific country from the provided list. I am utilizing the restcountries API ...

Tips for organizing data and dynamically linking options to another select value?

One of my challenges involves working with two select elements. The first select allows for multiple options, while the second select is dependent on the choice made in the first one. <select class="form-control" id="select1"> <option value=""& ...

What is the reason for Jest attempting to resolve all components in my index.ts file?

Having a bit of trouble while using Jest (with Enzyme) to test my Typescript-React project due to an issue with an alias module. The module is being found correctly, but I believe the problem may lie in the structure of one of my files. In my jest.config ...

Moving information from two modules to the service (Angular 2)

Recently in my Angular2 project, I created two components (users.component and tasks.component) that need to pass data to a service for processing before sending it to the parent component. Code snippet from users.component.ts: Import { Component } fro ...

Is there a definitive way to distinguish between scrollTop and scrollHeight in web development?

For instance, function checkingScroll(){ var newHeight = element.scrollHeight; var scrollTopValue = element.scrollTop; alert("The scrollHeight property is: " + newHeight + "px"); alert("The scrollTop property is: " + scrollTopValue ...

Navigate the orbit control camera in a circular motion around the target object

Just starting out in the world of threejs. I've managed to create a cube with different colors on each face. This cube can be rotated using OrbitControl, and I have 6 buttons in Dat.GUI that control the camera position. My goal is to click "Animate t ...

Synchronous XMLHTTPRequests have been marked as outdated

Recently, I faced an issue with a browser extension that required me to restart my browser. After the restart, I discovered that my browser (Chromium) had automatically updated to a new version that no longer supported synchronous AJAX requests. Here is wh ...

Tips for incorporating strikethrough into a drop-down select box in a jquery datatable and implementing filtering based on it

In this scenario, I am currently utilizing a jQuery Datatable with strikethrough text. My aim is to make the filterable dropdown display the same strikethrough text; however, it is not displaying properly at the moment. Below is my jQuery datatable setup: ...

Extracting numbers using regular expressions can be tricky especially when dealing with mixed

Currently, I am attempting to create a javascript regex that can extract decimal numbers from a string containing a mix of characters. Here are some examples of the mixed strings: mixed string123,456,00indeed mixed string123,456.00indeed mixed string123,4 ...

Searching for the nearest BBCode using JavaScript regex

After checking the suggested SO questions, none of them seem to address my issue. I have a small form textarea that allows for BBCODE formatting. For example: [url=http://www.derp.com/]link[/url] [url=http://www.derp.com/435]link[/url] When a link is hi ...

Transfer the values selected from checkboxes into an HTML input field

I am attempting to capture the values of checkboxes and then insert them into an input field once they have been checked. Using JavaScript, I have managed to show these values in a <span> tag successfully. However, when trying to do the same within a ...