我需要将字符串(或字符串数组)作为JSON对象发送到服务器,但是如果添加405 Method not allowed
参数和令牌,我会不断获取此Bad request
或csrf
。
这就是我想要做的:
var data = { 'info': $('#theData').html() }; // data is: <p> bla <strong>bla</strong> bla </p>
$.ajax({
type: 'POST',
contentType : 'application/json',
dataType : 'json',
url: "editInfo", // ?${_csrf.parameterName}=${_csrf.token}", // `Bad Request`
data: JSON.stringify(data),
success: function(data){
$('#responseMsg').html('YES!').show(200);
},
error: function(el) {
var msg = 'Error ' + el.status + ': ' + el.statusText;
$('#responseMsg').html(msg).show(200);
}
});
这是控制器:
@RequestMapping(value="editInfo", method = RequestMethod.POST, headers ="content-type=application/json")
public @ResponseBody String editInfo(
@RequestBody Info info,
HttpServletRequest request) {
// do stuff
return "success";
}
类
Info
只有1个名为info
的字符串,带有其getter和setter。理想情况下,您只想捕获String或String数组,但是在不获取该405 Method not allowed
的情况下任何有效的方法 最佳答案
嗯,一切看起来都很好,我无法重现-在添加CSRF令牌后,您的网址看起来如何?如果您设置静态参数(例如url:“ editInfo?param1 = csrf_token”),会得到405吗?
归根结底,问题可能是由于未正确定义控制器方法而发生的,因此它并不是真正的jQuery问题。
关于jquery - Spring MVC,Ajax发布JSON对象,不允许使用405方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32672162/