知道为什么我不能fetch()
在React Native中工作吗?完整代码位于RN Play上:https : //rnplay.org/apps/L80w2w
这是我正在尝试在React Native中工作的工作ReactJS小提琴:https ://jsfiddle.net/ssorallen/fEsYt/
componentDidMount: function() {
var _this = this;
var queryParams = "fn" + Date.now() + '/';
var queryUrl = "https://www.reddit.com/reddits.json?jsonp=" + queryParams;
fetch(queryUrl)
.then(response => response.json())
.then(responseData => this._handleResponse(responseData.data.children))
.catch(error => this.setState({
isLoading: false,
errorMessage: 'Something bad happened - loading navigationItems: \n' + error
}));
},
_handleResponse(results) {
this.setState({ isLoading: false });
if (results.length > 0) {
this.setState({
navigationItems: results
});
} else {
this.setState({ errorMessage: "Can't find JSON." });
}
},
您正在尝试使用JSONP技术来调用Reddit ,这是不必要的,因为React Native不会像浏览器那样强加Same-Origin安全策略。
如果您?jsonp=" + queryParams
从网址中删除参数,则请求应该可以正常工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句