不处理“PUT”请求

时间:2013-09-03 10:06:53

标签: java jquery spring http put

我有一个jquery脚本,它使用PUT类型将数据发送到我的spring控制器。但控制器永远不会被击中。 如果我将PUT更改为POST,一切都按预期工作,但我需要使用PUT。能否请您查看我的代码并告诉我我做错了什么?

的jQuery

var values = $("form").serialize();
                $.ajax({
                    type: "PUT",
                    url: "/user/" + elogin.val(),
                    async: false,
                    data: values,
                    success: function(resp) {\\doStuff}
                 })

控制器

@Controller
@RequestMapping(value = "/user")
public class RestController {

    @RequestMapping(value = "/{userLogin}", method = RequestMethod.PUT)
    @ResponseBody
    public boolean updateUser(@PathVariable String userLogin,
                              @RequestParam("epassword") String password,
                              ...)
            throws ParseException {
        if (...) {
            return false;
        }
        \\doStuff
        return true;
    }
}

FireBug错误消息

400 Bad Request
Response Headers
Connection  close
Content-Length  1072
Content-Type    text/html;charset=utf-8
Date    Tue, 03 Sep 2013 10:21:28 GMT
Server  Apache-Coyote/1.1

Request Headers
Accept  */*
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Connection  keep-alive
Content-Length  168
Content-Type    application/x-www-form-urlencoded; charset=UTF-8
Cookie  JSESSIONID=5A510B1FB82DA6F3DD9E9FA8D67A8295
Host    localhost:8085
Referer http://localhost:8085/welcome
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0
X-Requested-With    XMLHttpRequest

错误

HTTP Status 400 - Required String parameter 'epassword' is not present
type Status report
message Required String parameter 'epassword' is not present
description The request sent by the client was syntactically incorrect.

2 个答案:

答案 0 :(得分:2)

通过添加到web.xml

解决
<filter>
    <filter-name>HttpPutFormContentFilter</filter-name>
    <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>HttpPutFormContentFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

答案 1 :(得分:0)

您好我不知道是否正确但我认为我们不能将PUT用于jquery ajax

类型(默认:'GET') 类型:字符串 要求的类型(“POST”或“GET”),默认为“GET”。注意:此处也可以使用其他HTTP请求方法,例如PUT和DELETE,但并非所有浏览器都支持它们。

检查一下 http://api.jquery.com/jQuery.ajax/