From 440299e661235d34f5897b930f0932a6f0b7c413 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Mon, 28 Aug 2023 22:23:02 -0400 Subject: [PATCH] fix: don't try to parse response if request is head #11960 --- public/src/modules/api.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/public/src/modules/api.js b/public/src/modules/api.js index bfda975662..db8e0fb1b0 100644 --- a/public/src/modules/api.js +++ b/public/src/modules/api.js @@ -69,18 +69,23 @@ async function xhr(options, cb) { const isJSON = contentType && contentType.startsWith('application/json'); let response; - if (isJSON) { - response = await res.json(); - } else { - response = await res.text(); + if (options.method !== 'head') { + if (isJSON) { + response = await res.json(); + } else { + response = await res.text(); + } } if (!res.ok) { - return cb(new Error(isJSON ? response.status.message : response)); + if (response) { + return cb(new Error(isJSON ? response.status.message : response)); + } + return cb(new Error(res.statusText)); } cb(null, ( - isJSON && response.hasOwnProperty('status') && response.hasOwnProperty('response') ? + isJSON && response && response.hasOwnProperty('status') && response.hasOwnProperty('response') ? response.response : response ));