我在一个React Native组件中使用了一些代码,这是一个简单的fetch,将一个参数传递给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();
}但是,如果我对本地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();
}有什么想法吗?
发布于 2016-09-13 02:21:05
问题可能是应用程序传输安全,这是一个iOS特定的限制,(默认情况下)强制应用程序在所有网络通信中使用https。这是一个视频教程,解释了如何调整ATS,以便您的react原生应用程序可以工作:http://codecookbook.co/post/how-to-make-network-requests-in-react-native/
https://stackoverflow.com/questions/38138462
复制相似问题