Skip to content

Commit ce509eb

Browse files
committed
fetch images from API with CORS enabled
1 parent b673aab commit ce509eb

File tree

2 files changed

+3
-13
lines changed

2 files changed

+3
-13
lines changed

multiple-servers/api/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ func Run(config Config) {
6464
}
6565
// Indicate that what follows will be JSON
6666
w.Header().Add("Content-Type", "text/json")
67+
w.Header().Add("Access-Control-Allow-Origin", "*")
6768
// Send it back!
6869
w.Write(response)
6970
})

multiple-servers/assets/script.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
function fetchImages() {
2-
return Promise.resolve([
3-
{
4-
Title: "Sunset",
5-
AltText: "Clouds at sunset",
6-
URL: "https://images.unsplash.com/photo-1506815444479-bfdb1e96c566?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1000&q=80",
7-
},
8-
{
9-
Title: "Mountain",
10-
AltText: "A mountain at sunset",
11-
URL: "https://images.unsplash.com/photo-1540979388789-6cee28a1cdc9?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1000&q=80",
12-
},
13-
]);
2+
return fetch("http://localhost:8081/images.json").then(_ => _.json())
143
}
154

165
function timeout(t, v) {
@@ -21,7 +10,7 @@ function fetchImages() {
2110

2211
const gallery$ = document.querySelector(".gallery");
2312

24-
timeout(2000, fetchImages()).then(images => {
13+
fetchImages().then(images => {
2514
gallery$.textContent = images.length ? "" : "No images available.";
2615

2716
images.forEach(img => {

0 commit comments

Comments
 (0)