这是我的javascript。我正在试着发送PUT消息编辑。
$.ajax({
url: "/questions/" + _questionId + "/answers/" + answerId + "/edit",
method: "put",
data: {
id : answerId,
body : body,
"${_csrf.parameterName}": "${_csrf.token}"
}这是服务器代码。
@ResponseBody
@PreAuthorize("hasAuthority('ADMIN') or principal == #answer.content.user")
@RequestMapping(value = "/questions/{questionId}/answers/{answerId}/edit", method = PUT)
void editAnswer(@PathVariable(value = "questionId") Question question, @AuthenticationPrincipal CurrentUser currentUser,
AnswerForm answerForm, @PathVariable("answerId") String answerId) {
answerService.edit(answerForm, currentUser.getUser());
}而Controller是PUT方法
当我尝试POST方法时,它起作用了。但我只是更改了方法(PUT)服务器和客户端。无效的CSRF标记'null‘,在浏览器控制台上禁止使用403。
帮助。
发布于 2015-08-07 18:50:14
PUT请求正文中的CSRF标记似乎无法识别(Servlet容器不会从PUT请求正文生成参数映射)。
但您可以将其作为HTTP header发送
$.ajax({
url: "/questions/" + _questionId + "/answers/" + answerId + "/edit",
method: "put",
headers: { "${_csrf.parameterName}": "${_csrf.token}" },
data: {
id : answerId,
body : body,
}
});发布于 2016-09-15 10:10:47
请查看此链接:https://gist.github.com/ilake/3656567。我遇到了类似的问题。如果我使用POST方法,put <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />将会工作。但是,如果我在ajax中使用PUT方法。它不起作用。然后我使用headers方法。头部名称应为X-CSRF-TOKEN, not${_csrf.parameterName}`。请尝试此代码:
$.ajax({
url: "/questions/" + _questionId + "/answers/" + answerId + "/edit",
method: "put",
headers: { "X-CSRF-TOKEN": "${_csrf.token}" },
data: {
id : answerId,
body : body,
} });https://stackoverflow.com/questions/31875305
复制相似问题