0

如何从 Spring Controller 获取 json 响应到 JQueryAjax,
我正在尝试从 Controller 获取 json 数据但在 JQueryAjax 中出现错误 ststus,我的代码中有任何错误这是一个简单的登录应用程序这是我在 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;
    }

这是我的 JQueryAjax

数据.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] 警告 org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - 处理程序执行导致异常:找不到可接受的表示

4

1 回答 1

2

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>

参考:spring-3-mvc-and-json-example

于 2016-05-06T11:41:57.240 回答