ajax
请求```javascript function getCookie(name) { var cookieValue = null; if (document.cookie && document.cookie !== '') { var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = jQuery.trim(cookies[i]); // Does this cookie string begin with the name we want? if (cookie.substring(0, name.length + 1) === (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; } // 封装ajax请求的 function postData(data) { $.ajax({ 'url': '/cms/update_profile/', 'method': 'post', 'data': data, 'beforeSend': function (xhr, settings) { var csrftoken = getCookie('csrftoken'); //2.在header当中设置csrf_token的值 xhr.setRequestHeader('X-CSRFToken', csrftoken); }, 'success': function (data) { if (data.code == 200) { alert('修改成功'); } else { alert('修改失败'); } }, 'error': function (err) { console.error(err); } }) } ```
django-web开发中使用jquery提交数据处理csrf的初步封装
最新推荐文章于 2024-08-13 21:15:28 发布