菜鸟教程小白 发表于 2022-12-11 16:52:34

ios - React Native Network 请求在获取时失败 - 本地工作


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

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

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

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

<p>有什么想法吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>问题可能是应用传输安全性,这是 iOS 特定的限制(默认情况下)强制应用使用 https 进行所有网络通信。这是一个视频教程,解释如何调整 ATS 以使您的 react native 应用程序能够正常工作:<a href="http://codecookbook.co/post/how-to-make-network-requests-in-react-native/" rel="noreferrer noopener nofollow">http://codecookbook.co/post/how-to-make-network-requests-in-react-native/</a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - React Native Network 请求在获取时失败 - 本地工作,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38138462/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38138462/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - React Native Network 请求在获取时失败 - 本地工作