【发布时间】:2018-01-03 05:14:12
【问题描述】:
我尝试在 React Native 中向远程服务器发出请求,但总是得到TypeError: Network request failed;我尝试了不同的URL,发现如果我向http://rap2api.taobao.org/app/mock/3008/GET/api/creations这样的http URL发出请求,它总是失败;
如果我向 https://facebook.github.io/react-native/movies.json 之类的 https URL 发出请求,它会起作用;
如果我向http://localhost:3002/list 之类的 http localhost URL 发出请求,它也可以工作。
componentDidMount() {
console.log("_fetchData");
this._fetchData()
}
_fetchData = () => {
fetch('http://rap2api.taobao.org/app/mock/3008/GET/api/creations')
.then((response) => response.json())
.then((responseJson) => {
console.log('responseJson',responseJson)
})
.catch((error) => {
console.error(error);
});
}
我在网上查了很多参考资料,猜想这与 Info.plist doc 中的设置有关。我试着做一些修改,比如:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>localhost</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
<key>rap2api.taobao.org</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
或
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
但是,它们都不起作用,我在请求 http URL 时仍然遇到同样的错误。我确信 API 很好,因为我在 Web 应用程序上尝试过它并成功获得响应。有什么问题?
【问题讨论】:
标签: react-native