Receiving 406 error when attempting to send an object or integer from a Spring controller method using @ResponseBody to an AJAX request

Here is the code snippet I used in JavaScript:

function submit(){
    var form = $('#egform').serialize();
    alert("before ajax");
     $.post("testing.html", form, function(data, status) {
        if (data==1) {
            alert("Save successful");
        } else {
            alert("Failed");
        }
    });             
}

And this is my controller code:

@RequestMapping("testing.html")
public @ResponseBody Integer gettestvalue(HttpServletRequest request){
    String a=request.getParameter("first");
    System.out.println(a);
    return 1;
}

However, despite returning 1 from the controller, it is not being received by the JavaScript function. What am I missing?

Answer №1

Don't forget to include the necessary dependency in your project's pom.xml:

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
</dependency>

If you're not using Maven, ensure that you have added Jackson Mapper to your project.

Although it might not be essential for everyone, you may also require the core dependency:

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.13</version>
</dependency>

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

Browser crashes due to jQuery while loop issue

When using my jQuery code with a while loop to post data to a span ID, I am able to retrieve the span data but the post process crashes. while (ilkDeger < toplamDeger) { var yukle ="yukle"+ilkDeger ; var tekalan = &ap ...

What is the method to prevent the Submit Button from being active in CSS specifically on Firefox?

When I click this CSS in webpages on Chrome (OS: Windows), it works fine. However, Firefox (OS: CentOS7) does not seem to apply this CSS on webpages. How can I resolve this issue? Should I make adjustments in the CSS code below? #submit .btn.btn-primary ...

Encountered an npm installation error in the console, with a post-installation error message indicating code

I implemented the postinstall command in my Node project with React, but I encountered numerous errors during the installation process in React. npm WARN deprecated <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a8dbdcc9cac4cde ...

Is my front-end JavaScript fetch request mistakenly being sent as a GET instead of a POST?

On clicking the submit button, a fetch request is triggered. Strangely, in the developer tools, it shows up as a GET request. I tested the request using Insomnia and it returned the handlebars site to me without any of my console logs appearing on either ...

Add data to a size guide

My coding project involved creating a sizing chart using HTML, CSS, and JavaScript. The chart allows users to select their preferred units of measurement (metric or imperial). I used JavaScript to dynamically update the values in the chart based on the sel ...

Ways to resolve issues related to null type checking in TypeScript

I am encountering an issue with a property that can be null in my code. Even though I check for the value not being null and being an array before adding a new value to it, the type checker still considers the value as potentially null. Can anyone shed lig ...

Transforming the CanvasRenderer into a WebGLRenderer

I'm interested in implementing a visual effect similar to the one demonstrated in the example here: To achieve this, I want to modify the renderer to use WebGLRenderer instead of CanvasRenderer by changing: renderer = new THREE.CanvasRenderer(); to ...

Utilize mongoose-delete to bring back items that have been marked for deletion but are still

Whenever I remove an item from my list, it switches the properties of the data to true, marking it as deleted and moves it to the trash. However, when I try to restore the item from the trash, the deleted properties are no longer available and the data rea ...

Replace the use of $_SESSION with $.ajax

After launching the page, an $.ajax (POST) request is sent and the receiver.php file needs to modify the contents of the $_SESSION. However, it doesn't seem to be working. I'm not sure if the problem lies with the id of the $_SESSION being differ ...

Is it advisable to conceal the URL for a fetch request?

As I work on developing a React website, I've encountered the need to manage fetch request links differently for development and production environments. For example, using http://localhost:3000/users in development and in production. This setup is ...

Troubleshooting issues with jQuery getJson() in an ASP.NET MVC environment

I'm trying to utilize jQuery to make a simple call to my MVC controller. I've successfully tested the server side, as the controller correctly received the ajax call and returned the requested information. However, on the client side, the data is ...

Attempting to extract a specific item from an <li> element and then transfer that data into a text input field

Need help with a javascript issue involving an input box and a list of addresses. <input type="text" id = "addressbox" size="30" onkeyup="showResult(this.value)"> <ul class="livesearch" id="livesearch" style="border: 1px solid rgb(165, 172, 178) ...

Using a URL in an AJAX request

Before redirecting to another page, I am storing data from a textbox. When the user clicks the back button on the page load function in JavaScript, I retrieve the data from the textbox using: var pageval = $('#grid') .load('/Dealer/AllClai ...

Is there a way to retrieve the controller instance linked to a directive within the link function?

Is there a way to retrieve the controller instance connected with a directive within the link function? return { template: template, controller: controller, controllerAs: 'myCtrl', // What is the method for ac ...

What is the best way to automatically populate the date and time fields in PHP MySQL without manual input?

Is there a way to automatically populate these two input fields without any manual input? <input type="time" name="Time" id="Time" value="" /> I am looking for a solution to automatically fill the date and ti ...

Sending data from a bespoke server to components within NextJS

My custom server in NextJS is set up as outlined here for personalized routing. server.js: app.prepare() .then(() => { createServer((req, res) => { const parsedUrl = parse(req.url, true) const { pathname, query } = parsedUrl ...

make the text in em font size larger

Incorporating HTML dynamically into a page using JavaScript is shown in the example below. _strInnerHtml += "<em>" + value.cate_name + " | " + value.city_name + "</em>"; _strInnerHtml += "<a href='#' class='activi ...

How to host two Node.js socket.io projects on one URL

My Nodejs server is hosted on Amazon Lightsail. I have successfully connected to it and configured Apache for Nodejs. I am working with 2 Nodejs socket.io projects and I want to access both of them using a single URL. Below are the links in Nextjs: const ...

Comparing v-show(true/false) and replaceWith: Exploring the best practice between Vue and jQuery

Currently, I am in the process of learning vue.js and have a question regarding the best approach to implement the following scenario: https://i.sstatic.net/2YBEF.png https://i.sstatic.net/YCEHG.png The main query is whether it is acceptable in HTML to w ...

I require a web service that returns data in JSON format, and it should not contain any string or XML

I am currently developing a WebService Json Parser in C#/ASP.net using LINQ to SQL. The functionality is in place, however, the JSON output I am receiving includes string tags. <string>[{"cli_id":1,"nome":"Joe"},{"cli_id":2,"nome":"Gary"},{"cli_id": ...