OGeek|极客世界-中国程序员成长平台

标题: ios - React Native Network 请求在获取时失败 - 本地工作 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 16:52
标题: ios - React Native Network 请求在获取时失败 - 本地工作

我在 React Native 组件中使用了一些代码,这是一个将参数传递给 OMDB API 的简单获取。这可能是一个 CORS 问题,因为如果我以下面的格式运行它直接进入 omdbapi.com,它总是会失败,网络请求失败。但是,此请求在同一网络上的 Android 模拟器中有效。

   // The fetchData function makes an AJAX call to the OMDB API.
   fetchData(movieinput) {
     console.log("In fetch");
       // We pass the movie the user entered in into the URL for the API call.
       fetch('http://www.omdbapi.com/?t='+movieinput+'&y=&plot=short&r=json')
           .then((response) => response.json())
           .then((responseData) => {
               // After the data is recieved, we set this.state.movie to the result of the API call.
               this.setState({
                   movie: responseData,
               });
           })
           .done();
   }

但是,如果我将相同的代码运行到将远程请求包装到 localhost 请求中的本地 URL,则它可以正常工作。

   fetchData(movieinput) {
     console.log("In fetch");
       // We pass the movie the user entered in into the URL for the API call.
       fetch('http://localhost:3000/movie/'+movieinput)
           .then((response) => response.json())
           .then((responseData) => {
               // After the data is recieved, we set this.state.movie to the result of the API call.
               this.setState({
                   movie: JSON.parse(responseData),
               });
           })
           .done();
   }

有什么想法吗?



Best Answer-推荐答案


问题可能是应用传输安全性,这是 iOS 特定的限制(默认情况下)强制应用使用 https 进行所有网络通信。这是一个视频教程,解释如何调整 ATS 以使您的 react native 应用程序能够正常工作:http://codecookbook.co/post/how-to-make-network-requests-in-react-native/

关于ios - React Native Network 请求在获取时失败 - 本地工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38138462/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) Powered by Discuz! X3.4