什么是 $ {_ csrf.parameterName} 和 $ {_ csrf.headerName} 。如果我在发送之前没有 setRequestHeader(header,token),则会出错。
<input type="hidden" name="${_csrf.parameterName}"
value="${_csrf.token}" id="csrfToken" />
<meta name="_csrf_header" content="${_csrf.headerName}" />
<script>
function searchViaAjax() {
//get token and header
var token = $('#csrfToken').val();
var header = $("meta[name='_csrf_header']").attr("content");
var search = {}
search["username"] = $("#username").val();
search["email"] = $("#email").val();
$.ajax({
type : "POST",
contentType : "application/json",
url : "${home}search/api/getSearchResult",
data : JSON.stringify(search),
dataType : 'json',
timeout : 100000,
beforeSend : function(xhr) {
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader(header, token);
},
......
});
}
</script>