The AJAX code is malfunctioning

I am having trouble retrieving the origin IP address from the httpRequest object using the JavaScript code below. The xhttp.responseText is returning a null value. Any assistance would be greatly appreciated.

 <script type="text/javascript" language="JavaScript">
       var xhttp = new XMLHttpRequest();
       xhttp.onreadystatechange = function() {
           if (xhttp.readyState == 4 && xhttp.status == 0) {
              document.getElementById("LOCAL_IP").value = xhttp.responseText;
           }
       };
       xhttp.open("GET", "http://11.5.2.218:4080/getIP.jsp", true);
       xhttp.send();
  </script>

The content of the getIP.jsp file is as follows:

Your IP is <%=request.getRemoteAddr()%>

Answer №1

Greetings,

Make sure that the xhttp.status is set to 200, not 0.

To learn more about different server status codes, take a look at this HTTP status codes tutorial

Give this a try:

xhttp.onreadystatechange = function() {
  if (xhttp.readyState == 4 && xhttp.status == 200) {
    document.getElementById("LOCAL_IP").value = xhttp.responseText;
  }
};

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

The `readonly` html attribute prevents users from typing in text when included in the original html code, yet does not have the same effect when added dynamically through JavaScript

Similar Issue: How to apply "readonly" to <input> using jQuery This situation is quite perplexing, and I am hopeful for a straightforward solution. While my work involves Rails, I suspect this issue transcends framework specifics. Initially, se ...

Navigate to the previous or next page using JavaScript

I've been struggling to figure out how to navigate one page up and down using simple links, but I haven't found a solution that works for me. The URL I am working with is: . When the "next page" button is clicked, it should go to ?page=2, and whe ...

The Google Map is not showing all the details

Our app is developed using ReactJS on Rails API, with a Google map integrated. However, when visiting this link and clicking "Map View", some grey space appears under the Google Map. An example image can be found here: example We are having trouble findi ...

Dealing with event function inside object property: What's the best way?

Hello, I am currently using bootstrap-table for a web application where I need to redirect to another URL when a table row is clicked. The "onRowClick" events are managed through an object attribute known as onRowClick, where you assign it a function that ...

Having trouble with jQuery's find() function not working in conjunction with prev()?

I'm in need of some assistance because I think I may be approaching this the wrong way. Here is a link to the jsfiddle I have created: https://jsfiddle.net/m35o8jjL/2/ Below is the HTML code snippet: $( ".layered_subtitle_heading" ).click(function ...

Troubleshooting Material UI Widget Components

After working diligently on the autocomplete component, I am pleased with how the feature turned out. However, when this component is rendered as a widget on other pages, I'm encountering some style issues due to global styling overrides or additions ...

At what point is the JavaScript function expression triggered in this code snippet?

let express = require('express') let app = express(); app.use(express.static('static')); let server = app.listen(3000, function() { let port = server.address().port; console.log("The server has started on port", port); }); I ...

Add an exciting new element to the array within the DOM using knockout.js

Is there a way to make the font color change to red and then back to white when a new item is added to a play by play array using knockout? I know how to achieve this effect on a single property with the provided binding, but I'm unsure of how to do i ...

Remove console.log and alert statements from minified files using uglifyjs-folder

Currently, I am minifying multiple files in a directory using the uglifyjs-folder feature within my npm configuration in the package.json file as shown below: "uglifyjs": "uglifyjs-folder js -eyo build/js" The process is effectively minifying all the fil ...

Encountering an issue while attempting to utilize the .find() method in Mongoose

Currently, I am in the process of setting up a database using MongoDB and integrating the Mongoose ODM with Node.js. After running the code multiple times without any issues, I encountered an unexpected error when adding the last block to utilize the .find ...

What is the best way to add a key to a JavaScript array while keeping it reactive in Vue.js?

If there's an array in the state: state: { users: [] }, Containing objects like: { id: 1, name: "some cool name" } To add them to the store using a mutator like users.push(user);, how can we ensure that instead of 0:{...}, it uses the ...

retrieve data produced by JavaScript from an asynchronous request

Utilizing ajax to fetch a page $.get(url, function(content){ //ajax content }); Including my header file on the main page with global stylesheets and JavaScript libraries Also including the same file in the 2nd page loaded via ajax. Individually thes ...

Show the array's selected value in a Vuetify select input

I am working with a dataset containing city names and corresponding values. The goal is to display the value when a city is selected. I know that {{selected.value}} will give me the selected value, but I need to manipulate this value for calculations such ...

What is the best way to display an error message when a necessary field is left empty?

I am currently utilizing a plugin to validate my form. My goal is to display an error message on the button when clicked, as all fields in my form are required and need validation. Despite attempting the code below, it hasn't been successful: <Fo ...

Does anyone know of a way to integrate a calendar feature into a React application using a library

Greetings to everyone, I trust you are all enjoying a fantastic day. I am in search of an interactive calendar similar to this one for one of my applications https://i.sstatic.net/D3S3a.png Does anyone know of a React library that could assist me in crea ...

Learn the steps to integrate Infinite Scrolling with Node.js, Angular.js, and Firebase!

UPDATE 8: CODE: <% include ../partials/header %> <script src="https://www.gstatic.com/firebasejs/3.5.2/firebase.js"></script> <script src="https://cdn.firebase.com/libs/firebase-util/0.2.5/firebase-util.min.js"></script> & ...

When users click on the tiles, they will act as interactive objects that will direct them to a different page within the Angular framework,

output Here is the code for my app component: <H1>AI API DEMOS</H1> <div> <ul> <li class="active"><a routerLink="/market">Market Size</a></li> <li role="presentation&qu ...

Having trouble with transferring image blob to AWS S3 with node.js & angular

I'm having trouble uploading my images to AWS S3 { [InvalidParameterType: Expected params.Body to be a string, Buffer, Stream, Blob, or typed array object] message: 'Expected params.Body to be a string, Buffer, Stream, Blob, or typed arr ...

My locale NUXT JavaScript files are being blocked by Content Security Policy

I've been working on implementing CSP headers for my website to ensure data is loaded from trusted sources. However, I'm facing an issue where CSP is blocking my local JS files. Here's a snippet from my nuxt.config.js: const self = 'lo ...

How come the CSS styles I set for larger screens (1440px) are taking precedence over the ones I set for smaller screens (1280px)?

import styled from "styled-components"; import { devices } from "./devices"; export const HeaderContainer = styled.div` padding: 20px; @media ${devices.lg} { padding: 40px 90px; } @media ${devices.xl} { padding: 40px 2 ...