Skip to content

Commit

Permalink
fix: tidy up fetch init with request example
Browse files Browse the repository at this point in the history
  • Loading branch information
bsmth committed Apr 15, 2024
1 parent 6d7115e commit f5e3eb8
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions fetch/fetch-request-with-init/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@ <h1>Fetch Request with init example</h1>
<script>
const myImage = document.querySelector("img");

const myHeaders = new Headers();
const reqHeaders = new Headers();

const myOptions = {
method: "GET",
headers: myHeaders,
mode: "cors",
cache: "default",
// a cached response is okay unless it's more than a week old
reqHeaders.set("Cache-Control", "max-age=604800");

const options = {
headers: reqHeaders,
};

const myRequest = new Request("flowers.jpg", myOptions);
// pass init as an "options" object with our headers
const req = new Request("flowers.jpg", options);

fetch(myRequest)
fetch(req)
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP error, status = ${response.status}`);
Expand Down

0 comments on commit f5e3eb8

Please sign in to comment.