Frontend

Fetch-exempel

<script>
const data = {"Username": "Tjenargr4ht4h4t4he", "Password":"hejhej", "Price" : 800};
let token;
fetch('http://localhost:4000/Users/authenticate', {
method: 'POST', // or 'PUT'
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then(response => response.json())
.then(data => {
console.log(':)))) Success:', data);
token = data.token;
localStorage.setItem('authToken', token);
})
.catch((error) => {
console.error('Error:', error);
// doment.getElementbyId(”error-message”).innerHTML(error);
});
// Göra senare i koden: let myToken = localStorage.getItem('authToken');
/* Vid nästa fetch:
headers: {
'Content-Type': 'application/json',
'Autherization': localStorage.getItem('authToken');
},
*/
</script>

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.