簡體   English   中英

如何讀取正文/響應承諾中的標題

[英]How to read headers in body/response promise

我正在使用fetch API調用API端點。 如何在已解決的身體承諾中閱讀響應正文標題

我的代碼片段如下:

  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?
    });

如fetch 文檔中所述,您可以使用此代碼段獲取響應標頭:

  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!");
  }
});

對於身體,你會在這里找到一些例子。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM