On tab\browser close I need to send data to server. I found this answer (based on this blog) which recommends to use sendBeacon
. Here is shown how the data must be prepared in order to send them to server via Ajax. I repeated the structure from the answer:
window.addEventListener('unload', this.sendDataToServer, false)
sendDataToServer () {
myData = JSON.stringify({
'mutation': `mutation EmailAuth($email: String!, $password: String!) {
getOrCreateUser(email: $email, password: $password) {
token
}
}
`,
'variables': {email: 'test@test.net', password: 'Test'}
})
navigator.sendBeacon('http://localhost:8000/graphql', myData)
}
In this case Django shows: "POST /graphql HTTP/1.1" 400 53
I also found in internet the version with Blob
:
window.addEventListener('unload', this.sendDataToServer, false)
sendDataToServer () {
let headers = {
type: 'application/json'
}
let myBlob = new Blob([JSON.stringify({
'mutation': `mutation EmailAuth($email: String!, $password: String!) {
getOrCreateUser(email: $email, password: $password) {
token
}
}
`,
'variables': {email: 'test@test.net', password: 'Test'}
})], headers)
navigator.sendBeacon('http://localhost:8000/graphql', myBlob)
}
but in this case there is no any Django's reaction at all.
How can I wrap my data on a frontend side into GraphQL format and put it in sendBeacon
in the way, which can be accepted by server side?