我正在测试一个react原生应用程序(在xcode模拟器v9.2 / xcode 7.2.1中的OS X Yosemite上)。我在下面的代码中得到一个Network request failed
错误。具有正确appid的实际url在浏览器中运行良好,并以json格式给我正确的信息,promise / api调用看起来也很好。
我不是在防火墙后面。我已尝试排除连接故障,并在开发人员设置中激活Allow HTTP Services
,但仍收到错误。
你知道问题出在哪里吗?实际错误如下:
-- There has been a problem with your fetch operation: Network request failed
-- Api call error = Network request failed
下面是api.js代码:
var _ = require('lodash');
var rootUrl = 'http://api.openweathermap.org/data/2.5/weather?APPID=xxxxxxxxxxxxxxxxxxxxxx';
var kelvinToF = function(kelvin) {
return Math.round((kelvin - 273.15) * 1.8 + 32) + ' ˚F'
};
var kelvinToC = function(kelvin) {
return Math.round(kelvin - 273.15) + ' ˚C'
};
module.exports = function(latitude, longitude) {
var url = `${rootUrl}&lat=${latitude}&lon=${longitude}`;
console.log(url);
return fetch(url)
.then(function(response){
return response.json();
})
.then(function(json){
return {
city: json.name,
temperature1: kelvinToF(json.main.temp),
temperature2: kelvinToC(json.main.temp),
description: _.capitalize(json.weather[0].description)
}
})
.catch(function(error) {
console.log('There has been a problem with your fetch operation: ' + error.message);
throw error;
});
}
这是index.ios.js代码。
/* --depreciated
var React = require('react-native');
var {
AppRegistry,
MapView,
View,
Text,
StyleSheet
} = React;
*/
// updated
import React from 'react';
// updated
import {
AppRegistry,
MapView,
View,
Text,
StyleSheet,
} from 'react-native';
var Api = require('./src/api');
var Weather = React.createClass({
getInitialState: function() {
return {
pin: {
latitude: 0,
longitude: 0
},
city: '',
temperature1: '',
temperature2: '',
description: ''
};
},
render: function() {
return <View style={styles.container}>
<MapView
annotations={[this.state.pin]}
onRegionChangeComplete={this.onRegionChangeComplete}
style={styles.map}>
</MapView>
<View style={styles.textWrapper}>
<Text style={styles.text}>{this.state.city}</Text>
<Text style={styles.text}>{this.state.temperature1}</Text>
<Text style={styles.text}>{this.state.temperature2}</Text>
<Text style={styles.text}>{this.state.description}</Text>
</View>
</View>
},
onRegionChangeComplete: function(region) {
this.setState({
pin: {
longitude: region.longitude,
latitude: region.latitude
}
});
Api(region.latitude, region.longitude)
.then((data) => {
console.log(region.latitude);
console.log(region.longitude);
console.log('data = ' + data);
this.setState(data);
})
.catch((error)=>{
console.log("Api call error = ", error.message);
// alert(error.message);
});
}
});
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'stretch',
backgroundColor: '#F5FCFF'
},
map: {
flex: 2,
marginTop: 30
},
textWrapper: {
flex: 1,
alignItems: 'center'
},
text: {
fontSize: 30
}
});
AppRegistry.registerComponent('weather', () => Weather);
发布于 2016-08-10 12:34:52
您需要在info.plist中更改您的NSAppTransportSecurity策略。默认情况下,在ios 8和更大的明文请求被阻止。请参阅Transport security has blocked a cleartext HTTP
发布于 2016-08-11 03:27:09
好的,感谢@while1 1上面的答案,我找到了适合我的答案。
我上面问题中的代码是正常的。我不得不更改info.plist
文件。
哇,苹果真是个讨厌鬼,为什么他们会把事情搞得如此复杂呢?
我基本上在info.plist
文件中添加了以下内容。
<key>NSAllowsArbitraryLoads</key>
<true/>
如下所示:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>localhost</key>
<dict>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
在@while1 1上面的答案中的链接中可以看到更多关于这方面的信息。
发布于 2016-08-10 13:10:18
你的代码在我看来很熟悉,似乎你正在学习由Stephen Grider提供的react原生课程来构建应用程序。
就连我也有这个问题。
在api.js文件开头包括delete GLOBAL.XMLHttpRequest;
。
如下所示:
delete GLOBAL.XMLHttpRequest;
var _ = require('lodash');
var rootUrl = 'http://api.openweathermap.org/data/2.5/weather?APPID=xxxxxxxxxxxxxxxxxxxxxx';
https://stackoverflow.com/questions/38862837
复制相似问题