Transferring data from Tomcat Listener to a JavaScript function

I've been attempting to send objects from a Java Listener (not the JavaScript eventListener!) to a JavaScript application, but so far, I haven't had any success.

The specific requirement is that a JavaScript application running in the browser should request an object (such as an array) from a Listener upon launch, and the Listener should respond by providing the array. There are numerous ways to accomplish this task. What methods would be relatively secure and efficient?

Let's consider an example now. The JavaScript function can utilize jQuery to directly request the array from a JSON file (via HTTP GET request), and store its contents in a variable named 'data'. Here's how it can be done:

$.get("./myJSONfile.json", function( data ){
// Perform necessary operations
var myArray = data;
...
}

This can be achieved without involving any Servlet or Listener. How can I use a Listener to transfer the content of the JSON file to the JavaScript function?

Answer №1

Not quite sure what you're asking, but one common method of communication between JavaScript and a server is using an AJAX call to retrieve data. Here's an example:

var dataArray = [];
$.ajax({
    url: "serverURL",
    async: false,
    type: "GET",
    error: function (jqXHR, textStatus, errorThrown) {
        alert(jqXHR + "-" + textStatus + "-" + errorThrown);
    },
    success: function (data) {
        dataArray = data;
    }
});

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

Having trouble with the Google Places API integration using Python

Hey there! I'm currently experimenting with the Google Places API in a rather unique way - trying to fetch a list of all McDonald's locations in Germany. I know, it's a bit unusual! Below is the code I've put together for this task (the ...

Oops! Gulp encountered an error: Assertion Error [ERR_ASSERTION]: You must specify a task function

Being new to javascript, I attempted to execute some repositories from GitHub. After installing all the required updates and running npm audit --force, I am still encountering this error. Any assistance would be greatly appreciated. Error Code : PS D:&bso ...

What is the process for selecting the Node version when installing with .nvm?

I am having an issue with nvm in the terminal. When I try to run npm install <something>, it always installs the package in node version 9.4.0, regardless of the version set using nvm. Even after switching to node version 10.15.3 using nvm use v10.1 ...

Empty req.body object in Node.js

I came across this code snippet: const bodyParser = require("body-parser"); const express = require("express"); const app = express(); app.use(express.json()); app.use(bodyParser.json()); app.post("/api/foo", (request, response) => { let foo = { ...

Users should always receive a notification, whether it be a success message or an

When I send data to my json and it returns true, a success message should be displayed. If it does not return true, an error message should be shown. The current issue is that the response should indicate whether it was successful or encountered an error. ...

What is the best way to connect individual buttons to a dynamic div that displays different content depending on the button clicked?

Hey there! I'm diving into the world of JavaScript and HTML, and I need some guidance on creating a menu that can toggle visibility of specific content in div(s) depending on which button (picture1-12) is clicked. My idea is to have one div that can d ...

Obtaining numerous files in base64 along with their respective file names using FileReaderAPI in Javascript

When I upload 3 items named png1, png2, and png3, the result will be as follows: alert 1 png1 / base64 string conversion alert 2 png2 / base64 string conversion alert 3 png3 / base64 string conversion I experimented with this code snippet. fu ...

Encountering Internal Server Error when running Node-Express app on render.com with query parameters live

Currently, I am facing an issue while attempting to execute a live route with query using my nodejs express application on render.com. Strangely, all other routes connected to the crud operations are functioning properly except for the search filter route ...

Implementing pagination links to trigger image changes on click

I have a javascript function that swaps the image source when clicked. I am looking to incorporate pagination links in this script. Specifically, I want to include "Previous" and "Next" text links to navigate between images. Can someone help me figure out ...

Minimizing conditional statements in my JavaScript code

I've just completed the development of a slider and am currently working on optimizing some repetitive if/else statements in the code. The purpose of these conditions is to determine whether the slider has reached the last slide, in which case the nex ...

Issue with Checkbox Functionality Between Parent and Child Components in React.js

In the main component, I have four checkboxes. My goal is to display a value from a function in the child component based on whether each checkbox is checked or not. export default class App extends Component { constructor(props) { super(props); ...

Can the SVG def element be modified?

In my SVG file, there is a defs element structured as follows: <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 250 330" onclick="tweenGloss()"> <defs> <linearGradient id="grad" x1="174.6 ...

An issue with event listeners in Vue 3 and Pixi.js arises when working with DisplayObjects that are either created as properties of classes or inherited from parent classes

Utilizing PIXI js in conjunction with Vue 3 (see code snippet below) Due to the consistent pattern of most graphics with varying behaviors and properties, we opted for an OOP approach with TypeScript to prevent code duplication. However, following this app ...

Setting the texture for a loaded glb model using Three.js

After successfully loading a basic glb model created in SketchUp using Three.JS, I encountered an issue with displaying text on the model. The model includes a group identified as Text. Despite being able to load and visualize the model correctly in Three ...

Retrieve the width of an element once the browser has finalized its dimensions

I am facing an issue with centering a pop-up box perfectly within the window using CSS properties. The code for the pop-up box styling is as follows: #pop_up { position: fixed; display: inline-block; border: 2px solid green; box-shadow: 0p ...

What is the most effective way to extract "sub" values from a Python dictionary?

How can I effectively retrieve values from a Python dictionary? The full function and dictionary code are provided below Context Upon researching, it seems that using the get() method should work. However, my current code only returns "None" for the "ti ...

"Encountering a problem with using setState in React Hook useEffect

I am currently utilizing the useState hook with two arrays: imageList and videoList. In my useEffect hook, I iterate through the data using forEach method. If the type of the item is an image, it should be pushed to the imageList array. However, after exec ...

Ways to efficiently populate several dropdown menus with jQuery by making a single request to a local JSON file

I am running into an issue with the Country/City dropdowns in my project. Every time I try to change the countries, I encounter an unexpected behavior. How can I efficiently fetch the local JSON file to populate multiple dropdowns with just one request? Ta ...

Tips for verifying elements using the Loop technique in a JSON array

I am new to JavaScript and I have been trying to run the following code with an expected result like this: [["00:04:12","05:54:46"],["06:06:42","12:45:22"],["12:51:11","15:56:11"]] However, my script is not working as expected. Can someone please help ...

Switching a react virtualized table from JavaScript to TypeScript can uncover some type-related challenges

I have implemented the following demo in my React project: https://codesandbox.io/s/react-virtualized-table-checbox-stackoverflow-rbl0v?fontsize=14&hidenavigation=1&theme=dark&file=/src/App.js However, I am encountering issues with the code sni ...