CSRF 적용된 Django 서버에 axios 이용하여 POST 요청하기

2020. 11. 4. 11:24실전

1.

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
var getCookie = function(name) {
	var value = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
	return value? value[2] : null;
}

header = {
	headers: {'X-CSRFToken': getCookie('csrftoken')}
}
data = {}
var url = 'example.com'
axios.put(url, data, header)
	.then(res => {
		console.log(res)
	})
</script>

2.

axios.interceptors.request.use(request => {
	request.headers.delete['Content-Type'] = "application/x-www-form-urlencoded"
	request.headers.delete['X-CSRFToken'] = getCookie('csrftoken')
	return request
})

data = {}
var url = document.location.origin
	+ '/' + APPNAME + '/site/' + this.site.uid
axios.delete(url, data)
	.then(res => {
		console.log(res)
	})