首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >fetch - Works Local上的React Native Network请求失败

fetch - Works Local上的React Native Network请求失败
EN

Stack Overflow用户
提问于 2016-07-01 14:27:26
回答 1查看 448关注 0票数 2

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

代码语言:javascript
复制
   // 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();
   }

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

代码语言:javascript
复制
   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();
   }

有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2016-09-13 02:21:05

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38138462

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档