以下のようにレスポンスヘッダーのcontent-type
を確認することで検証できます。
async function fetchSample(request) {
try {
const response = await fetch(request);
const contentType = response.headers.get("content-type");
if (!contentType || !contentType.includes("application/json")) {
throw new TypeError("JSONではありません。");
}
const jsonData = await response.json();
// ...
} catch (error) {
console.error("Error:", error);
}
}