Response は Fetch API のインターフェイスで、リクエストのレスポンスを表します。
Response オブジェクトは Response.Response() コンストラクターを用いて生成することができますが、他の API 操作の結果として返される Response オブジェクトに出会う可能性が高いでしょう。例えば、サービスワーカーの Fetchevent.respondWith や、単純な WindowOrWorkerGlobalScope.fetch() などです。
画像をリクエストするとき、レスポンスに正しい MIME タイプを設定するために、Response.blob を実行する必要があることに注意してください。
const image = document.querySelector('.my-image');
fetch('flowers.jpg')
.then(response => response.blob())
.then(blob => {
const objectURL = URL.createObjectURL(blob);
image.src = objectURL;
});
const response = new Response();
// Function to do an Ajax call
const doAjax = async () => {
const response = await fetch('Ajax.php'); // Generate the Response object
if (response.ok) {
const jsonValue = await response.json(); // Get JSON value from the response body
return Promise.resolve(jsonValue);
} else {
return Promise.reject('*** PHP file not found');
}
}
// Call the function and output value or error message to console
doAjax().then(console.log).catch(console.log);
| 表示内容: |
| 表示内容: |