2016-05-22 7 views
3

fetch APIを使用してAPIエンドポイントを呼び出しています。 本文およびヘッダーを解決済みの約束で読むにはどうすればよいですか?本文/応答約束のヘッダーを読む方法

以下の私のコードスニペット:あなたは見つけるでしょうについては

fetch(myRequest).then(function(response) { 
    var contentType = response.headers.get("content-type"); 
    if(contentType && contentType.indexOf("application/json") !== -1) { 
    return response.json().then(function(json) { 
     // process your JSON further 
    }); 
    } else { 
    console.log("Oops, we haven't got JSON!"); 
    } 
}); 

を:

fetch(url, { 
     credentials: 'include', 
     method: 'post', 
     headers: { 
     "Content-Type": "application/json; charset=utf-8", 
     }, 
     body: JSON.stringify({ 
     email: email, 
     password: password, 
     }), 
    }) 
    .then(response => response.json()) 
    .then(function(response) { 
     // How to access response headers here? 
    }); 

答えて

-1

としては、あなたがこのスニペットでレスポンスヘッダを取得することができます

、フェッチdocumentationに言いましたhereいくつかの例。

+2

JSONを処理するヘッダーを取得する必要があります。 – LukasMac

+0

私の例では、jsonメソッドのcontentTypeに到達できます – Ygalbel

関連する問題