Skip to content

Instantly share code, notes, and snippets.

@jecyhw
Last active December 22, 2016 07:05
Show Gist options
  • Save jecyhw/643b5eff81df523c0fbe3a69dad36c3f to your computer and use it in GitHub Desktop.
Save jecyhw/643b5eff81df523c0fbe3a69dad36c3f to your computer and use it in GitHub Desktop.
spring boot ajax request with csrf
// <meta name="_csrf" th:content="${_csrf.token}"/>
// <meta name="_csrf_header" th:content="${_csrf.headerName}"/>
$(function () {
var token = $("meta[name='_csrf']").attr("content");
var header = $("meta[name='_csrf_header']").attr("content");
$(document).ajaxSend(function(e, xhr, options) {
xhr.setRequestHeader(header, token);
});
});
//<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment