首页 > 解决方案 > 如何在 react-native 中禁用 ssl 证书验证

问题描述

我正在用 react-native 构建一个应用程序。我正在尝试使用 fetch 方法在 react-native 中使用 API。SSL 证书验证在关闭时会从 API 给出响应,否则会显示“无响应”。有没有办法在 react-native 中禁用 SSL 证书验证?下面是我点击 API 的代码片段。

fetch(('https:www.myUrl.com',{
       method: 'GET',
       headers:{
         'Accept': 'application/json',
         'Content-Type': 'application/json',
       },
}))
     .then((response) => response.json())
     .then((responseJson) => {
          console.log(JSON.stringify(responseJson))
     })
     .catch((err) => {
          Alert.alert("Request Failed")
          console.log(err.toString())
     });

标签: javascriptnode.jsreact-nativessl

解决方案


您将 fetch 的两个参数的括号合为一个,请考虑以下内容

fetch('https://slimparts.cap-tek.com:8080/v1/make', {
    method: 'GET',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
    }
  })
  .then((response) => response.json())
  .then((responseJson) => {
    console.log(JSON.stringify(responseJson))
  })
  .catch((err) => {
    console.log(err.toString())
  });
  
  


推荐阅读