离子获取远程JSON数据 [英] Ionic getting remote JSON data

查看:113
本文介绍了离子获取远程JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过将来自JSON链接的数据作为对象来尝试以下代码。一切都很好。但是,当我真正从服务器远程检索它并进行测试时,我似乎无法将其显示在列表中。有人可以帮忙吗?
我想在home.html页面上列出标题,图片和作者。感谢是否有人可以提供帮助。仍在努力学习。



我尝试过获取其他JSON $ http.get('



这通常是因为如果您要求使用与您的网页不同的域名,则浏览器会阻止您的安全考虑请求。根据我的经验,您可以使用 jsonp 作为您的请求数据类型或添加 Access-Control-Allow-Origin 标头到服务器的响应。



参见此问题以获取更多信息


I tried the following code by coping the data from JSON link as an object. Everything was good. But when I really retrieve it remote from the server and test, it seems that I cannot get it display on the list. Can someone help ? I want to list the title,image and author on the home.html page. Appreciate if anyone can help. Still trying to learn.

I've tried getting other JSON $http.get('https://jsonplaceholder.typicode.com/posts') and it worked. But this particular one is a little difficult for me to understand why it is not parsing correctly. For the code to work, i need to have it on my phone in order to work also. I can see the JSON being downloaded and alerted. But, I don't know why still not seeing the data.

Please see my code as follows. http://codepen.io/ccrash/pen/VjNPkv

Home.html

<ion-view view-title="Dashboard">
  <ion-content class="padding">
    <h2>Ionic GET Json Example</h2>
      <ion-item ng-repeat="x in result[0].data.items">
      <div> {{x.title}} </div>
      <div> {{x.image[0]}} </div>
      <div> {{x.author}} </div>
    </ion-item>
  </ion-content> 
</ion-view> 

Controller

.controller('HomeCtrl', function($http, $scope, $ionicSideMenuDelegate) {

  $http.get('http://www.malaysiakini.com/c/en/news?alt=json')
    .success(function(data, status, headers,config){
      //console.log('data success');
      //console.log(data); // for browser console
      alert(JSON.stringify(data));
      $scope.result = data; // for UI
    })
    .error(function(data, status, headers,config){
      console.log(headers); 
      console.log('data error');
    })
    .then(function(result){
      things = result.data;
    });  

})

解决方案

It's a common issue. I see errors in browser console:

It's usually because you are request to a different domain than your page is on, browser will block your request for security consideration. According to my experience, you can use jsonp as your request data type or add Access-Control-Allow-Origin headers to the server's response.

See this question for more infomation

这篇关于离子获取远程JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆