我如何在获取呼叫中解析简单的json响应



我在提取响应中获得了简单的json对象。当我尝试访问它时,它被抓住了。

this my reposnse
{
    "islogin": true
}

 fetch(url, data)
      .then(res => {console.log(res);return res.json()})
      .then(
        (result) => {
          console.log("rsult",JSON.stringify(result));
          console.log(result.islogin);
          console.log("1");
          console.log("authentication success");
        })
      .catch((error) => { window.alert("error", error);
        console.log("authentication failed");
      });

我期望结果。Ilislogin为True/false。

fetch(URL, {
    method: "POST", // "GET/POST"
    headers: {
        "Content-Type": "application/json"
    },
    body: JSON.stringify(data)
})
.then(r => r.json())
.then(r => {
   console.log('Response', r) // You will get JSON response here.
}).catch(error => console.error('Error', error))

我遇到了类似问题,因为缺少内容类型的标题,有时由于不串制请求有效负载。尝试此代码,看看它是否有效。上面是我用于项目的工作片段。

最新更新