JavaScript required
We’re sorry, but Coda doesn’t work properly without JavaScript enabled.
Skip to content
Gallery
Handbook Backend
Huvudmoment
API-gränssnitt
Protokoll
Säkerhet
Databaskoppling
Fullstack
Agilt Arbetssätt
(Extra)
More
Share
Explore
Frontend
Javascript
Vanilla (fetch)
Cache
Asp Dot Net
Fetch med Bearer
Backend Dag 7 Frontend.
pdf
1.9 MB
Backend Dag 8 Frontend Forts.
pdf
2.4 MB
<
script
>
function
fetchAuth () {
// (A) URL & CREDENTIALS
var
url =
"http://localhost:4000/users"
,
credentials =
"testa"
;
// (B) FETCH WITH HTTP AUTH
fetch (url, {
headers: {
"Authorization"
:
`Basic
${
credentials
}
`
}
})
// (C) SERVER RESPONSE
.then((result)
=>
{
if
(result.status != 200) {
throw
new
Error(
"Bad Server Response"
); }
return
result.text();
})
.then((response)
=>
{
document.getElementById(
"demoShow"
).innerHTML = response;
})
// (D) HANDLE ERRORS (OPTIONAL)
.catch((error)
=>
{ console.log(error); });
}
</
script
>
<
div
id=
"demoShow"
></
div
>
<
input
type=
"button"
value=
"Fetch Auth"
onclick=
"fetchAuth()"
/>
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
Ctrl
P
) instead.