首页 > 解决方案 > 为什么 post man 中的 api 请求和 react native 中的 api fetch 请求有不同的响应

问题描述

  fetch(`${valid_url}`, {
              method:"GET",
              headers:{
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like 
 Gecko) Chrome/51.0.2704.47 Safari/537.36"
              }
            
            }).then((response) => response.text())
            .then((result) => {
             console.log(result)
             ...

标签: reactjsreact-native

解决方案


添加有效标题

    fetch("Add URL Here", {
      method: "GET",
      headers: {
      //  Add valid header here
     Accept: "application/json",
    "Content-Type": "application/json",
      },
    })
      .then((response) => response.json())
      .then((responseJson) => {
           API Responce here
        console.log(responseJson);
        //  setState(responseJson.VAlue);
      })//
      .catch((error) => {
        alert(error);
      });

推荐阅读