2015-11-07 77 views
2

任何想法,为什么我不能得到fetch()在工作反应本土?完整的代码在这里RN播放: https://rnplay.org/apps/L80w2w不能得到`取()`工作中反应原住民

下面是我试图让工作在阵营本地工作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." }); 
     } 
    },  
+0

无法使用rnplay链接,请在snack.expo.com中创建一个新链接 – locropulenton

回答

4

您正在尝试使用JSONP技术来调用书签交易,这是不必要的,因为React Native不会像浏览器那样施加同源安全策略。

如果您从您的网址的?jsonp=" + queryParams参数,请求应该工作。

相关问题