java - Ajax 将请求发送到 Spring MVC

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

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

@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的 Controller 中:

@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 中添加了 jackson 依赖项:

<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 并将其添加到我的表单 <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/> 中)

最佳答案

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

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

并将其添加到您的 html 中:

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

关于java - Ajax 将请求发送到 Spring MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38556133/

相关文章:

java - 升级到新的 Jenkins 版本后,构建失败

jquery - 在 IE8/9 中使用 jQuery 和 XDomainRequest 的 CORS

javascript - 使用替换方法替换innerHTML以及在Javascript中不起作用的正则表达式

php - 如何在jquery中使用Ajax传递带有值的数组并在Php页面中获取值

javascript - 从 AJAX post 访问 C# Controller 中的表单数据

java - 在 Feign RequestInterceptor/RequestTemplate 中访问 URITemplate 或 RequestLine 值

java - 无法在框架 'java AWT' 中添加简单的复选框

java - 为什么java断言没有效果?

php - 从mysql返回数据到html

javascript - AJAX 表单提交后永无止境的 "Connecting"消息