qiGuar
qiGuar

Reputation: 1804

"PUT" request isn't processed

I have a jquery script that sends data to my spring controller using PUT type. But controller never gets hit. If i change PUT to POST everything works as expected, but i need to use exactly PUT. Could you please review my code and tell me what i am doing wrong?

jQuery

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

Controller

@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 error message

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

Error

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.

Upvotes: 2

Views: 692

Answers (2)

qiGuar
qiGuar

Reputation: 1804

Solved by adding to 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>

Upvotes: 2

dharmesh
dharmesh

Reputation: 308

Hi I dont know am correct or not but I think we cannot use PUT for jquery ajax

type (default: 'GET') Type: String The type of request to make ("POST" or "GET"), default is "GET". Note: Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers.

check this http://api.jquery.com/jQuery.ajax/

Upvotes: 0

Related Questions