我正在使用SpringBoot 1.2.3.RELEASE,它是一个使用百日咳和jquery的简单SpringMVC Web应用程序。
我的控制器:
@Controller
@RequestMapping(value="/cart")
public class CartController
{
@RequestMapping(value="", method=RequestMethod.GET)
public String showCart(HttpServletRequest request, Model model)
{
Cart cart = getOrCreateCart(request);
model.addAttribute("cart", cart);
return "cart";
}
@RequestMapping(value="/items", method=RequestMethod.PUT)
public String updateCartItem(@RequestBody LineItem item, HttpServletRequest request, HttpServletResponse response)
{
Cart cart = getOrCreateCart(request);
cart.updateItemQuantity(item.getProduct(), item.getQuantity());
return "redirect:/cart";
}
}
我正在使用jquery发送PUT请求,如下所示:
$.ajax ({
url: 'cart/items',
type: "PUT",
dataType: "json",
contentType: "application/json",
data : '{ "product" :{ "sku":"'+ sku +'"},"quantity":"'+quantity+'"}',
success: function(responseData, status, xhttp){
alert(responseData);
//location.reload();
}
});
当触发此PUT请求时,其到达updateCartItem()方法,然后使用return "redirect:/cart";
,它会抛出PUT http://localhost:8080/cart 405(Method Not Allowed)错误。
为什么重定向视图为重定向网址带有相同的PUT方法?
我在405 JSP error with Put Method
看到类似的内容我怎样才能以春季方式解决这个问题?
答案 0 :(得分:0)
尝试:
$.ajax({
url: 'cart/items',
type: 'POST',
data: {
_method: 'PUT'
},
success: function(data) {
...
}
});
在springmvc web.xml中添加一些行
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<servlet-name>springmvc</servlet-name>
</filter-mapping>