SimpleDao

在js中添加代码

  1. $(document).ready(function() {
  2. // using jQuery
  3. function getCookie(name) {
  4. var cookieValue = null;
  5. if (document.cookie && document.cookie !== '') {
  6. var cookies = document.cookie.split(';');
  7. for (var i = 0; i < cookies.length; i++) {
  8. var cookie = jQuery.trim(cookies[i]);
  9. // Does this cookie string begin with the name we want?
  10. if (cookie.substring(0, name.length + 1) === (name + '=')) {
  11. cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
  12. break;
  13. }
  14. }
  15. }
  16. return cookieValue;
  17. }
  18. var csrftoken = getCookie('csrftoken');
  19. function csrfSafeMethod(method) {
  20. // these HTTP methods do not require CSRF protection
  21. return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
  22. }
  23. $.ajaxSetup({
  24. beforeSend: function(xhr, settings) {
  25. if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
  26. xhr.setRequestHeader("X-CSRFToken", csrftoken);
  27. }
  28. }
  29. });
  30. });