# django-csrf-ajax

> A JavaScript utility for acquiring and including Django's CSRF token in AJAX request headers.

Latest version **1.0.1** (published 2018-11-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install django-csrf-ajax
pnpm add django-csrf-ajax
yarn add django-csrf-ajax
bun add django-csrf-ajax
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2018-11-06 |
| First published | 2018-11-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 97.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Rick Jordan |
| Maintainers | rickjordan |
| Keywords | ajax, csrf, django |

## Links

- npm: https://www.npmjs.com/package/django-csrf-ajax
- Homepage: https://github.com/rickjordan/django-csrf-ajax
- npm.io page: https://npm.io/package/django-csrf-ajax

## Recent versions

- 1.0.1 (latest) — 2018-11-06
- 1.0.0 — 2018-11-06

## README

# django-csrf-ajax
A JavaScript utility for acquiring and including Django's CSRF token in AJAX request headers.

Currently supports the following HTTP libraries:
* axios
* jQuery

## Installation

via [npm](https://www.npmjs.com/package/django-csrf-ajax)...
```
$ npm install django-csrf-ajax
```

## Setup

Node and CommonJS build systems...
```javascript
var token = require('django-csrf-ajax')
```

Direct browser use...
```html
<script src="./node_modules/django-csrf-ajax/dist/token.js"></script>
<script>
// access the `token` global...
</script>
```

## Setting the Token Headers

Initialize once on page load, prior to making any AJAX requests...

```javascript
// axios
var axios = require('axios')
token.setTokenHeader('axios', axios)

// jQuery
var $ = require('jquery')
token.setTokenHeader('jquery', $)
```

`django-csrf-ajax` will extract the CSRF token value from the browser's cookies and set it as a default CSRF header for all CSRF-safe request methods of the library provided (e.g., POST, PUT, PATCH, etc.).

To access the token directly...
```javascript
var csrfToken = token.getTokenFromCookie(token.defaults.COOKIE_NAME)
```

Default values can be customized prior to setting the token headers, like so...

```javascript
token.defaults = {
    HEADER_NAME: "custom-header", // default: "X-CSRFToken"
    COOKIE_NAME: "custom-cookie" // default: "csrftoken"
}
```

## Further Reading

* [Django CSRF Protection](https://docs.djangoproject.com/en/dev/ref/csrf/)

---
_Source: https://npm.io/package/django-csrf-ajax · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
