Ajax提交csrf_token

  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="UTF-8">  
  5.     <title>Title</title>  
  6.     <script src="/static/dist/js/jquery-3.1.1.js"></script>  
  7. </head>  
  8. <body>  
  9. <form action="/icbc.html" method="post">  
  10.     {% csrf_token %}  
  11.     <input type="text" name="from">  
  12.     <input type="text" name="to">  
  13.     <input type="text" name="money">  
  14.     <input type="submit" value="转账">  
  15.     <onclick="ajaxSubmit();">Ajax提交</a>  
  16. </form>  
  17. <script>  
  18.     function ajaxSubmit() {  
  19.         $.ajax({  
  20.             url: 'icbc.html',  
  21.             type: 'POST',  
  22.             data: {'k1': 'v1', 'k2': 'v2', 'csrfmiddlewaretoken': $('input[name="csrfmiddlewaretoken"]').val()},  
  23.             success: function (arg) {  
  24.                 console.log(arg)  
  25.   
  26.             }  
  27.         })  
  28.     }  
  29. </script>  
  30. </body>  
  31. </html>  
posted @ 2017-09-22 15:17  xp5  阅读(154)  评论(0编辑  收藏  举报