【发布时间】:2015-09-10 09:34:03
【问题描述】:
Spring MVC 和 Ajax 方面的菜鸟。我正在尝试使用 jQuery Ajax 帖子将一些数据从视图发送到控制器,但它要么给我 405 handleHttpRequestMethodNotSupported 或 403 :-( 有人能指出我正确的方向吗?
这是我的控制器操作 preAuthorize。
@PreAuthorize( "hasAuthority('Admin')" )
@RequestMapping( value = "/configSave", method = RequestMethod.POST )
public String saveConfiguration( @RequestParam String test )
{
return "configSave";
}
以下是发送发布请求的页面中的代码。
...
<input type="button" value="Save" id="save" onclick="saveConfig()"/>
...
function saveConfig()
{
var testPost = { test: "testing"};
$.ajax(
{
url: "hello/configSave",
data: "{'test':'testing'}",
contentType: "application/x-www-form-urlencoded",
type: "post",
beforeSend: function (xhr) {
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
},
success: function () {
alert("foo");
},
error: function() {
alert("bar");
}
}
);
}
它所做的只是向我抛出一个带有bar 警报的 403(暂时,否则为 405)错误。
编辑:
令人惊讶的是,当我不执行 ajax 发布(我需要这样做)时,它会在控制器中调用 configSave 操作。只是真的对这种行为感到困惑!
【问题讨论】:
-
headers="Accept=application/json" 尝试在你的 requestMapping 中添加这个
-
@DejanBiljecki - 不,也不使用。
标签: jquery ajax spring-mvc http-status-code-405