【问题标题】:Ajax Post request to Spring MVCAjax Post 请求到 Spring MVC
【发布时间】:2016-11-28 02:38:06
【问题描述】:

我在我的应用程序中使用了 ajax 和 spring mvc。当用户点击注册按钮时,下面的代码在我的控制器类中工作,名为 UserController:

@RequestMapping(value = "/registration", method = RequestMethod.GET)
public ModelAndView registrationPage() {
    ModelAndView model = new ModelAndView();
    model.setViewName("registration");
    return model;
}

注册页面(registration.jsp) 包含一个表单。我在此页面中使用了 ajax 请求。 下面是jquery ajax代码:

function ajaxPostForUsername() {
var contextPath = "<%= request.getContextPath()%>";
var username = $('#userName').val();
$.ajax({
        type: 'POST',
        url: contextPath + "/registration",
        data: {username: username},
        dataType: 'json',
        contentType : "application/json",
        success: function(response) {
                console.log(response);
                if (response == "USER_EXIST") {
                    $('.username-message').html("Username is exist.Please enter another!").css('color', 'red')
                    }
                else {
                    $('.username-message').html("Valid username").css('color', 'green');
                    }
            }
    })

}

用法如下:

<input type="text" id="userName" name="userName" placeholder="User Name" onblur="ajaxPostForUsername()" class="form-control"  autofocus>

在名为RegistrationController的控制器中:

@RequestMapping(value = "/registration", method = RequestMethod.POST)
@ResponseBody
public String validateUsername(@RequestBody String username) {
    String jsonData = "USER_NOT_EXIST";
    if (userService.isUsernameExist(username)) {
        jsonData = "USER_EXIST";
    }
    return jsonData;
}

像这样。我在 pom.xml 中添加了杰克逊依赖项:

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

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

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

所以当我在用户名字段中写一个名字时,响应是:

HTTP Status 405 - Request method 'POST' not supported

java 控制台显示:

WARNING: Handler execution resulted in exception: Request method 'POST' not supported.

如您在图片中看到的那样,请求看起来是正确的。 Request-Response

我已经搜索过,但无法解决。那么谁能告诉我是什么问题? (PS:我使用了spring security并在我的表单中添加了这个&lt;input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/&gt;

【问题讨论】:

    标签: java jquery ajax spring-mvc http-status-code-405


    【解决方案1】:

    你必须将此添加到您的 ajax 请求中

    beforeSend: function(xhr) {
                        xhr.setRequestHeader(header, token)
                      },
    

    这是你的 html :

    <meta name="_csrf" content="${_csrf.token}"/>
        <meta name="_csrf_header" content="${_csrf.headerName}"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-24
      • 2014-03-28
      • 2023-03-17
      • 1970-01-01
      • 2015-08-08
      • 2015-04-28
      • 2021-08-09
      • 2012-11-11
      相关资源
      最近更新 更多