- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>
- Title
- </title>
- </head>
- <body>
- <form action="/login" method="post">
- <!--{% raw xsrf_form_html() %}-->
- <input type="text" name="message" />
- <input type="submit" value="Post" />
- </form>
- <input type="button" value="Ajax CSRF" onclick="SubmitCsrf();" />
- <script src="jquery-3.1.1.js">
- </script>
- <script>
- function getCookie(name) {
- var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
- return r ? r[1] : undefined;
- }
- function SubmitCsrf() {
- var nid = getCookie('_xsrf');
- $.post({
- url: ' / csrf',
- data: {'k1': 'v1',
- '_xsrf': nid
- },
- success: function(callback) {
- console.log(callback);
- }
- })
- }
- </script>
- </body>
- </html>
来源: http://www.bubuko.com/infodetail-1984440.html