This is usually caused by Spring's default CSRF protection.
If you use, for example, a DELETE HTTP request from your JS code, you must also send CSRF security headers.
No need to disable CSRF protection! Please do not do this if not necessary.
You can easily add CSRF AJAX / REST protection:
1. Adding meta headers to each page (use @ layout.html or something else):
<head> <meta name="_csrf" th:content="${_csrf.token}"/> <meta name="_csrf_header" th:content="${_csrf.headerName}"/> </head>
2. By setting up your ajax requests to send these headers for each request:
$(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); }); });
Note that I use thymeleaf, so I use th: content instead of the content attribute.
lukyer
source share