From f945c899ee5f89131c2498257ee6128df56061fa Mon Sep 17 00:00:00 2001 From: peter_rabbit <pierrejarriges@gmail.com> Date: Thu, 21 Jan 2021 17:52:14 +0100 Subject: [PATCH] fix fetch json --- public/games/games.js | 8 ++++++-- public/main.js | 8 ++++++-- src/lib/fetch.js | 8 ++++++-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/public/games/games.js b/public/games/games.js index 1fe170a..d919a2a 100644 --- a/public/games/games.js +++ b/public/games/games.js @@ -166,7 +166,9 @@ module.exports = { function fetchjson(url) { return new Promise((resolve, reject) => { - fetch(url, { mode: "no-cors" }) + const headers = new Headers(); + headers.append("Accept", "application/json"); + fetch(url, { mode: "no-cors", headers }) .then(r => r.json()) .then(r => resolve(r)) .catch(e => reject(e)); @@ -174,8 +176,10 @@ function fetchjson(url) { } function fetchtext(url) { + const headers = new Headers(); + headers.append("Accept", "text/html"); return new Promise((resolve, reject) => { - fetch(url, { mode: "no-cors" }) + fetch(url, { mode: "no-cors", headers }) .then(r => r.text()) .then(r => resolve(r)) .catch(e => reject(e)); diff --git a/public/main.js b/public/main.js index 9acdbd6..4db5922 100644 --- a/public/main.js +++ b/public/main.js @@ -435,7 +435,9 @@ module.exports = { function fetchjson(url) { return new Promise((resolve, reject) => { - fetch(url, { mode: "no-cors" }) + const headers = new Headers(); + headers.append("Accept", "application/json"); + fetch(url, { mode: "no-cors", headers }) .then(r => r.json()) .then(r => resolve(r)) .catch(e => reject(e)); @@ -443,8 +445,10 @@ function fetchjson(url) { } function fetchtext(url) { + const headers = new Headers(); + headers.append("Accept", "text/html"); return new Promise((resolve, reject) => { - fetch(url, { mode: "no-cors" }) + fetch(url, { mode: "no-cors", headers }) .then(r => r.text()) .then(r => resolve(r)) .catch(e => reject(e)); diff --git a/src/lib/fetch.js b/src/lib/fetch.js index 46a964f..4ad85bd 100644 --- a/src/lib/fetch.js +++ b/src/lib/fetch.js @@ -2,7 +2,9 @@ function fetchjson(url) { return new Promise((resolve, reject) => { - fetch(url, { mode: "no-cors" }) + const headers = new Headers(); + headers.append("Accept", "application/json"); + fetch(url, { mode: "no-cors", headers }) .then(r => r.json()) .then(r => resolve(r)) .catch(e => reject(e)); @@ -10,8 +12,10 @@ function fetchjson(url) { } function fetchtext(url) { + const headers = new Headers(); + headers.append("Accept", "text/html"); return new Promise((resolve, reject) => { - fetch(url, { mode: "no-cors" }) + fetch(url, { mode: "no-cors", headers }) .then(r => r.text()) .then(r => resolve(r)) .catch(e => reject(e)); -- GitLab