-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.vue
47 lines (39 loc) · 1008 Bytes
/
app.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<template>
<div>
<h1>Form Without Token</h1>
<form method="post" action="/api/test">
<input type="submit" value="Submit" />
</form>
<h1>Form With Token</h1>
<form method="post" action="/api/test">
<input name="_csrf" type="hidden" :value="csrf" />
<input type="submit" value="Submit" />
</form>
<h1>AJAX Without Token</h1>
<button @click="onClick('')">Submit</button>
<h1>AJAX With Token</h1>
<button @click="onClick(csrf)">Submit</button>
</div>
</template>
<script setup lang="ts">
import { FetchOptions } from 'ohmyfetch'
import { useCSRFToken } from '../src/runtime/composables/useCSRFToken'
const csrf = useCSRFToken()
function onClick(token: string) {
const opts: FetchOptions = {
method: 'POST',
}
if (token !== '') {
opts.body = {
_csrf: token,
}
}
$fetch('/api/test', opts)
.then(() => {
console.log('success')
})
.catch((e) => {
console.log(e.data.message)
})
}
</script>