0

How to get json response from Spring Controller to JQueryAjax,
I am trying to get json data from Controller but getting error ststus in JQueryAjax, Have any error in my code this is a simple login application This is my Controller in Spring

HomeController.java

    @RequestMapping(value = "/login.htm", method = RequestMethod.POST)
    public @ResponseBody User loginUser(HttpServletRequest request, HttpServletResponse response, User ub)
    {

        String email = request.getParameter("txt_email");
        String password = request.getParameter("txt_password");

        ub.setEmail(email);
        ub.setPassword(password);

        UserServiceImpl us = new UserServiceImpl();
        User ub1=new User();
        ub1= us.verifyUserLogin(ub);

        return ub1;
    }

This is my JQueryAjax

data.js

    function usersignin(url)
    {
        var val = signin_validate();
        if (val == false)
        {
            return;
        }
        var email = $('#txt_email').val();
        var password = $('#txt_password').val();
        var formData =
        {
                'txt_email' : email,
                'txt_password' : password,

        };
        $.ajax(
                {
                    type : 'POST',
                    url : url,
                    data : formData,
                    dataType: "json",

                    success : function(res, textStatus)
                    {
                        var msg="Succesfully..! Login";
                        showAlertLogin(msg);
                        window.location.href='index.jsp'            

                    },

                    error : function(res, textStatus)
                    {
                        var msg="Failed..! Login";
                        showAlertLogin(msg);
                        window.location.href='layout.jsp'           

                    }
                });

    }

I added dependency file

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

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

But i didnt get json response in ajax only getting error why? One error in console is

[http-nio-8080-exec-5] WARN org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Handler execution resulted in exception: Could not find acceptable representation

3
  • Please try dataType : 'Json' in ajax calling. Commented May 6, 2016 at 11:25
  • @NikhilGupta I added but not working Commented May 6, 2016 at 12:25
  • @Nikhil I added in my js but not working Commented May 6, 2016 at 12:26

1 Answer 1

2

Starting with Spring 3.1, a the @RequestMapping annotation now has a produces specifically for this purpose:

@RequestMapping(value = "/login.htm", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody User loginUser(HttpServletRequest request, HttpServletResponse response, User ub)
    {

        String email = request.getParameter("txt_email");
        String password = request.getParameter("txt_password");

        ub.setEmail(email);
        ub.setPassword(password);

        UserServiceImpl us = new UserServiceImpl();
        User ub1=new User();
        ub1= us.verifyUserLogin(ub);

        return ub1;
    }

Add Dependency:

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

        <!-- Jackson JSON Mapper -->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.10</version>
        </dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.5.1</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.5.1</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.5.1</version>
</dependency>

References:spring-3-mvc-and-json-example

Sign up to request clarification or add additional context in comments.

14 Comments

Igot error in Jquery and error in console too the error is this one [http-nio-8080-exec-8] WARN org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Handler execution resulted in exception: Could not find acceptable representation
you have jar dependency problems
I mention my dependency in my question.Please help me.Without Json data its work fine?
I am using Spring 4.2.2.RELEASE i added entire dependency file
have you added <mvc:annotation-driven /> ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.