Skip to content

Instantly share code, notes, and snippets.

@caramelchocolate
Created June 4, 2019 06:55
Show Gist options
  • Save caramelchocolate/d5a9f93127006c830b1fc02ecdce8641 to your computer and use it in GitHub Desktop.
Save caramelchocolate/d5a9f93127006c830b1fc02ecdce8641 to your computer and use it in GitHub Desktop.
djangoでjquery(javascript)でのajaxを使う場合
// ref: https://docs.djangoproject.com/en/2.2/ref/csrf/#ajax
// 下記の関数のソースは上記の公式ドキュメントを参考
var csrftoken = Cookies.get('csrftoken');
$.ajaxSetup({
headers: {
'X-CSRFToken': csrftoken
}
});
$.ajax({
type: 'POST',
url: request_url,
dataType: "json"
}).done((data, textStatus, jqXHR) => {
// ...
}).fail( (data) => {
// ...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment