Tips for providing the URL in JSON using Spring MVC

Every time I try to run my code, I encounter an issue where I cannot access the URL specified in the getJSON function.

Below is my controller code:

@RequestMapping(value = "branch")
@Controller
public class BranchController {
@Autowired(required = true)
VillageService villageService;


 @RequestMapping(value = "/addHome", method = RequestMethod.GET)
 public @ResponseBody 
 List getForm1(@RequestParam("districtId") int districtId, Model model, 
  HttpServletRequest request, HttpServletResponse response) {
    try {

        villageList= villageService.getDistrictVillageList(districtId);


    } catch (Exception er) {
        log.error("error in addLoanType=" + er);
    }

    return villageList;

  }

Here is my JavaScript code snippet:

<script>
        $(document).ready(function() {
            $("select#district").change(function() {
              $.getJSON("/addHome", {districtId: $(this).val()}, function(j) {
                   var options = '';
                   for (var i = 0; i < j.length; i++) {
                   options += '<option value="' + j[i].id + '">' +                
                            j[i].name + '</option>';
                        }
                                  $("select#village").html(options);
                });
            });

        });

    </script>   

I am facing a problem with my code. Can someone please assist me in resolving this issue?

Answer №1

Your controller mapping indicates that your URL should be:

$.getJSON("branch/addHome.json",...);

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

Analyzing JSON information within a tabular format

JSONObject category;JSONArray eventsArray;JSONObject eventData; JSONArray contactsArray;JSONObject contactData; JSONObject jsonObject=new JSONObject(data); categories = jsonObject.getJSONArray("categories"); for (int i = 0; ...

Customizing Passport JS Bearer strategy for various endpoints and HTTP REST operations

I'm currently using the BearerStrategy and I am attempting to configure different strategies for each endpoint or method within the same router. After reviewing the documentation, I have not come across any references to this particular scenario othe ...

Unable to resolve 500 error on Vercel in Next.js despite successful local development

Here is the content of route.ts import fs from 'fs'; import path from 'path'; import PizZip from 'pizzip'; import Docxtemplater from 'docxtemplater'; import { NextRequest, NextResponse } from 'next/server'; ...

Error: Cannot access 'addEventListener' property of null in Chrome Extension due to TypeError

I've been working on developing a chrome extension that autofills input forms in web pages. However, I encountered an error that says "cannot read properties of null." This issue arises when I try to add an event listener to a button in my code. The f ...

Is it necessary to enable validation for an Angular reactive form toggle?

Can you help with this issue I'm having? I have a radio button that asks the user if they have a massage certificate. If they answer yes, a file input should be displayed. By default, the file input should not be required, but if the user selects yes, ...

The tab indicator in Material-UI fails to update when the back button is clicked

My code is currently functioning well: The tab indicator moves according to the URL of my tab. However, there is a peculiar issue that arises when the back button of the browser is pressed - the URL changes but the indicator remains on the same tab as befo ...

Retrieve an element within a jQuery each loop

I'm currently implementing AJAX functionality to retrieve cart items from the server and display them within a cart when a customer clicks on the "My Cart" button. Here is the model for the cart: public class Cart { [Key] public i ...

Placing content retrieved from a MySQL database within a form displayed in a ColorBox

Having difficulty inserting text from a MySQL database into a form (textfield) inside a ColorBox. The current script is as follows: <a href="#" class="bttn sgreen quote1">Quote</a> var postQuote[<?php echo 4id; ?>]=<?php echo $f ...

Unable to load the PDF file in Angular 8 due to an error

I've been attempting to display a PDF in a new browser tab using Angular 8. The PDF is retrieved from a Spring Boot API as a byte array: @GetMapping(value = "/pdf") public ResponseEntity<byte[]> beve(HttpServletRequest request) { return new ...

You cannot nest a map function within another map function in React

Having some trouble applying the map function in HTML using React. Below is the code snippet: response = [ data : { name: 'john', title: 'john doe', images: { slider: { desktop: 'link1', mo ...

Adding property to an object retrieved from an API in a React application can be achieved effortlessly by utilizing the useState

How can I implement a toggle functionality for Bookmarked Meals on my Meal Recipe Website available at ? I am currently using the logic to set data.meals[0].bookmarked to true or false, but I want to use setState instead in order to rerender the page when ...

Shifting the entire page from left to right and back again

I am in search for a template that moves the page from left to right. If anyone can guide me on how to create this effect or provide a JavaScript example, I would greatly appreciate it. ...

Tips for using tooltip.format() to access an array of objects in anychart.js

Having difficulty displaying an array of objects in the tooltip of an Anychart.js map. I know we can access the dataset using %[name of property in data set]. The structure of my dataset is as follows: { "country": "Austria", &q ...

The Spring API is recommended to output JSON data instead of returning an escaped String

I need to configure my Spring endpoint to return JSON without escaping double quotes so it can be collapsed/expanded in Chrome. Is there a way to specify to Spring that the string in the message is actual JSON representation? Here is the declaration of th ...

Exiting node.js gracefully using PM2

I have a node application running with pm2, and I need to save data before the application closes. The following code works perfectly in the shell: process.on('exit', function(){ log.debug('exit'); }); process.on('SIGINT&apos ...

Issue encountered while attempting to display the image within the table in Swift 3

I am encountering an issue where the image URL is empty when trying to transform it into an image inside a table cell. Can anyone provide assistance with this problem? Below are my two classes. import UIKit // code snippets and implementations here ...

Utilize focusout and onclick events on multiple components at the same time

Currently, I am in the process of coding an Autocomplete feature from scratch in Vue, but I am encountering a challenge when it comes to selecting an option from the dropdown menu. I have set it up so that the dropdown is shown when the input is clicked ...

Tips for executing a function in the HC-Sticky plugin?

Currently, I am utilizing the HC-Sticky JavaScript plugin and endeavoring to utilize the documented reinit method. However, I am facing difficulty in understanding how to execute it. In this CodePen demo, a basic setup is displayed along with an attempt t ...

The NextJS development server is frequently losing connection

Issue While working on my NextJS project, I've been experiencing occasional disruptions with my development server. Whenever I update my code, I encounter random occurrences of the dev server disconnecting and displaying the error message below: Type ...

Optimal method for creating a seamless loop of animated elements passing through the viewport

My challenge involves managing a dynamic set of elements arranged next to each other in a row. I envision them moving in an infinite loop across the screen, transitioning seamlessly from one side to the other, as illustrated here: https://i.stack.imgur.com ...