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

How to modify a record in an array within MongoDB by leveraging webMethods

I need to find a specific document in an arrayList, with the following JSON string: { "_id" : ObjectId("58b9339502be203f6b476664"), "_docType" : "Test", "type" : "mongoUpdate", "createdDateTime" : "2017-03-03 09:12:53.080", "contacts" : [ ...

Connecting a text input in a nested component to the data passed down from its parent component in VueJS

My VueJS setup involves a child component sending data to a parent component, which then tries to route the data to a sibling of the child to create new components. I'm using a dictionary to group the data and push it into an array. The array is then ...

What is the best way to have a text field automatically insert a hyphen after specific numbers?

Is there a way to make a text field insert hyphens automatically after certain numbers? For example, when typing a date like 20120212, could we have the input automatically formatted with hyphens after the first 4 digits and the second two, so it displays ...

The significance of API Input Validation and Steering Clear of Lengthy Conditional Statements

Currently, I am working on ensuring that my API functions correctly even in cases of bad or missing data. At the moment, I have an if statement that checks for any missing inputs. If an input is missing, it returns false, otherwise there is a large else b ...

What is the method to store and retrieve data attributes linked to elements such as select options within the React framework?

Despite my efforts, I'm currently unable to retrieve the data attribute from an option using React as it keeps returning null. <select onChange={(e) => this.onIndustryChangeOption(e)} value={this.props.selectedIndustry}> <opti ...

axios interceptor - delay the request until the cookie API call is completed, and proceed only after that

Struggling to make axios wait for an additional call in the interceptor to finish. Using NuxtJS as a frontend SPA with Laravel 8 API. After trying various approaches for about 4 days, none seem to be effective. TARGET Require axios REQUEST interceptor t ...

Save the raw InputMask data in Formik with Material-UI

I currently have Input Mask implemented with a customized Material UI text field inside a Formik form: <InputMask mask="999-99-9999" maskChar="X" value={values.ssn} ...

Guide on attaching an event to every dynamically created element in JavaScript

I'm currently generating 'li' elements dynamically using a loop and running into issues when it comes to assigning events to each generated element. My goal is to assign an onclick event to every li element that is created. Check out the co ...

Issue with LendingClub.com API causing 500 Error when attempting to purchase Notes in the Secondary Market

While I have successfully been able to make API requests with other endpoints, I am experiencing difficulties specifically with the endpoint for making buys. It appears to be a server error on their side, but just to double-check, I wanted to see if anyone ...

What could be causing the strange output from my filtered Object.values() function?

In my Vue3 component, I created a feature to showcase data using chips. The input is an Object with keys as indexes and values containing the element to be displayed. Here is the complete code documentation: <template> <div class="row" ...

Comparing values in an NSDictionary on iOS

section: An important task at hand involves retrieving and storing two distinct sets of JSON data as NSArray and NSDictionary, respectively. Here is a glimpse into the structures of these JSON data: ( { Cells = ( { ...

Is there a way to access dynamic keys on an object in jq?

As I work on creating my own custom filter functions, one of the challenges I am facing is how to pass a list of strings to a filter and retrieve the corresponding values from the input object. Here's an example: jq -n '{name: "Chris", ...

Analyzing and refreshing the data entries in firebase database

https://i.stack.imgur.com/ZMjck.png I have been attempting to modify my Username password group name. However, the update process is not successful. I am looking for a way to compare data before updating fields, but I am unable to find a suitable method. ...

Implementing Vuejs sorting feature post rendering

Currently, I have elements linked to @click event listeners. <th @click="sort('dateadded')" class="created_at">Date Added I am looking for a way to automatically trigger this sorting function when the Vue.js component renders, so that th ...

Keep the first column of an HTML table fixed as you scroll horizontally

Below is an HTML table that I have created: <table id="customers" class="table table-bordered" width="100%"> <thead> <tr> <th colspan="2">Activities</th> <th>Mon 17</th> <th>Tue 18</th> </thead> ...

Tips for concealing a div when the mouse is moved off it?

My goal is to create a simple hover effect where hovering over an image within a view filled with images displays an additional div. This part works as expected. However, I'm facing issues when trying to hide the same div when the user moves out of t ...

Issues with AngularJS ng-bind-html failing to display list items

I am working with a basic HTML document that looks like this... <ol> <li><strong>Test 1</strong></li> <li><strong>Test 2</strong></li> </ol> ...and I am attempting to connect it to a div ...

Function executed many times during click action

I have developed a web application that allows users to input any keyword or statement and receive twenty results from Wikipedia using the Wikipedia API. The AJAX functionality is working perfectly. The app should dynamically create a DIV to display each r ...

React - method for transmitting dynamically generated styles to a div element

As a newcomer to the world of React, I keep encountering an unexpected token error related to the ":". Can someone please assist me with understanding the correct syntax for including multiple styles within the Box component provided below? Additionally, h ...

Deleting the stylesheet exclusively within the confines of the React application window

Here is an image that will help illustrate the issue: https://i.stack.imgur.com/VA7fw.png If you want to check out the code sandbox for this problem, you can visit: https://codesandbox.io/s/annoying-stylesheet-2gpejc?file=/public/index.html I am current ...