Having difficulty obtaining JSON response on my JSP page

After implementing some JavaScript validation in JSP, I have come across an issue with my AJAX function. Despite receiving a response from the request (as verified through F12 developer tools), I am unable to view the output.

How can I effectively extract and read the JSON object response within my JSP code?

 $.ajax({
       type: "GET",
       url: "AjaxActionController?",
       dataType: "json",
       async : false,
       data:"leave_Type="+leave_Type,
       Success: function(data){
           alert(data);
           return_Leave = data.LeaveType;
           alert(return_Leave);

Answer №1

Remember to use "success" instead of "Success".

Give this code a try:

$.ajax({
   type: "GET",
   url: "AjaxActionController?",
   dataType: "json",
   data: "leave_Type="+leave_Type,
   success: function(data){
       alert(data);
       return_Leave = data.LeaveType;
       alert(return_Leave);
   }
});

Please take note:

  • If the server specifies application/json as the value for the Content-Type header, jQuery will automatically convert the response into a JavaScript object without needing the dataType property.
  • We recommend removing async: false to make the request asynchronous. Synchronous requests are not recommended as they freeze the page and prevent user interaction while waiting for a response from the server.

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

Print directly without the need for a preview or dialog box

Is there a way to easily print content with just one click, without having to go through the preview or print dialog box? Here is a snippet of my code: <head> <style type="text/css"> #printable { display: none; } @media print { #non-pr ...

Gremlin, can you tell me where the "valueMap()" function has been imported from in JavaScript?

Working with ES6 on Node.js, I am attempting to execute the project() step within a Gremlin query. For this projection, my goal is to extract the properties. In the Gremlin console, I would typically use valueMap() to retrieve these properties. However, ...

converting a JSON object into an array

I'm facing a challenge and unsure how to proceed. I have received JSON data from an API: https://i.stack.imgur.com/GdDUo.png When I log the data, this is what appears in the console: https://i.stack.imgur.com/GjSPW.png My goal is to extract the id ...

Is there a way to pass a token variable from the main page to an iframe without relying on a post message?

I am seeking assistance on how to transfer a variable from a parent window to an iframe. My situation is as follows: I am working with 2 Angular5 applications named A and B . Application B is loaded within Application A using an iframe. The aut ...

Customized Values in AngularJS Slider

My slider currently has values ranging from 1 to 10, but I want to change them to 1 Lakh, 2 Lakhs, 3 Lakhs, 5 Lakhs, 10 Lakhs, and more than 10 Lakhs. How can I adjust the value set to display this text instead? I have incorporated rg-slider into my code ...

The input type "number" does not seem to be compatible with the currency pipe feature

The currency pipe seems to not be working when using the input type number. The web page is not displaying any value. I have searched various blogs and it appears that the currency pipe only works with input type text, but my requirement necessitates the ...

What is the best method for transforming a JavaScript array of Objects to a different format?

Is there a way to transform this array of objects from one format to another? The original array is as follows: const data = [ {name: 'Customer 1', value: {Oil: 55, Gas: 66, Retail: 56}}, {name: 'Customer 2', value: {Oil: ...

Emphasize the center row within a moving table

I am interested in developing a scrolling table where only 10 rows are visible at any given time, with the middle row set to stand out even during scrolling. The concept is that as the user scrolls down, the highlighted row changes progressively as they c ...

I encountered a validation error and a 404 error while trying to input data into all fields. Kindly review and check for accuracy. Additionally, I have included an

click here for image description Despite providing all details in the form fields, I keep receiving an error message prompting me to enter all fields... I am also encountering a "/api/user 404 Not Found" error and unsure of the reason. Interestingly, wh ...

Is it possible to incorporate an external javascript file in my CSS file?

Currently, I am attempting to create a setup where my background adjusts based on the width of the user's browser. However, I am constrained by a background specified in the external stylesheet under a specific element. While I have the ability to alt ...

What is the method for adding all columns of JQuery data obtained from a Linq query to a Div element?

I'm currently working on a controller ActionResult method that fetches data from the server in JSON format. I have some Ajax and jQuery code in the view to retrieve the JSON data and append it to a container. Previously, I had a controller ViewResult ...

Can Angular be used to send form data to an external URL?

Let's take a look at an example with some code: <form method="post" action="URL"> <input type="text" name="first name" /> <input type="text" name="last name"/> <input type="submit" value="Submit" name="submit"/> < ...

Populate Vue.js component with data prior to rendering

I'm new to using Vue.js and I've been struggling to create a simple component that includes a select list. I've been attempting to populate the options data by simulating an Ajax request, here is my code: HTML <div id="app"> < ...

Using Django forms within a modal: dynamically redirecting and managing errors

My current project involves integrating a login form within a modal window using Django. I found a helpful guide at this link which has been working effectively so far. However, I am now aiming to enhance the user experience by redirecting them to their pe ...

Utilizing parameters and variables as function arguments in reactjs

I have created a function type React component: function Card(props, {icon}) { return ( <div className="card"> <FontAwesomeIcon icon={icon} className="icon"/> <h3 className="text">{props.name}</h3&g ...

Use jQuery's .each method to reiterate through only the initial 5 elements

Is there a way to loop through just the initial 5 elements using jQuery's each method? $(".kltat").each(function() { // Restrict this to only the first five elements of the .kltat class } ...

I am facing a challenging issue with React on my console and finding it difficult to resolve

Below is the code snippet from my ShoppingList.js file, ` function ShoppingList() { return ( <ul className="lmj-plant-list"> {plantList.map(({ id, cover, title, description }) => ( <PlantItem to={"/ ...

The ASP.NET session ends prematurely before reaching the session timeout limit

Our ASP.NET 4.0 web application is currently running on IIS 7.0 within a load balanced environment. We have implemented a system where certain sections of the webpage are updated every 5 minutes using AJAX UpdatePanel and web services. The goal is to keep ...

passing parameters to the URL in Vue.js

Using Vue.js 3, I am working on filtering products. My goal is to send parameters to the URL for filtering and I am utilizing Vue Router for this purpose. At the moment, my filter only sends one parameter to the URL from each group. However, I aim to appen ...

I am interested in updating the content on the page seamlessly using Angular 6 without the need to reload

As a newcomer to Angular, I am interested in dynamically changing the page content or displaying a new component with fresh information. My website currently features cards, which you can view by following this Cards link. I would like to update the page ...