如何从Spring Controller获取JSON响应到JQueryAjax?

时间:2016-05-06 07:53:56

标签: spring spring-mvc jquery-ajaxq

如何从Spring Controller获取json响应到JQueryAjax,
我试图从控制器获取json数据但在JQueryAjax中获取错误ststus, 我的代码中有任何错误,这是一个简单的登录应用程序     这是我在Spring的Controller

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;
    }

这是我的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'           

                    }
                });

    }

我添加了依赖文件

    <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>

但是我没有在ajax中获得json响应只得到错误原因? 控制台中的一个错误是

[http-nio-8080-exec-5] WARN org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - 处理程序执行导致异常:无法找到可接受的表示

1 个答案:

答案 0 :(得分:1)

Spring 3.1 开始, @RequestMapping 注释现在具有生成,专门用于此目的:

@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;
    }

添加依赖关系:

  <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