Edit:
I am new to hybrid development.I referred this Sample to parse json in listview using ionic2. But when I run the code, I could see only the blank screen in browser.
Below I have posted the codes. Please check :
pages.ts:
import { Component } from '@angular/core';
import {Http} from '@angular/http';
import { NavController } from 'ionic-angular';
import 'rxjs/add/operator/toPromise';
@Component({
selector: 'page-home',
templateUrl: 'pages.html'
})
export class SlidingPage {
public items:any;
constructor(public navCtrl: NavController,public http: Http) {
this.http = http;
this.http.get("http://api.randomuser.me/?results=10")
.subscribe(data =>{
// console.log(data['_body']);
// this.items=JSON.parse(data['_body']).results;//Bind data to items object
this.items = data.json();
},error=>{
console.log(error);// Error getting the data
} );
}
buttonClick(event){
console.log("button clicked");
console.log(event);
}
itemClicked(event,itemData){
console.log("item clicked");
console.log(event);
console.log(itemData);
}
}
Pages.html:
<ion-header>
<ion-navbar>
<ion-title>
List View
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-list>
<ion-item *ngFor="let item of items" (click)="itemClicked($event,item)">
<ion-avatar item-left>
<img src="{{item.picture.thumbnail}}">
</ion-avatar>
<h2>{{item.name.first | uppercase }}</h2>
<h3>{{item.gender}}</h3>
<ion-icon *ngIf="item.gender=='female'" name="woman" item-left></ion-icon>
<ion-icon *ngIf="item.gender=='male'" name="man" item-left></ion-icon>
<ion-icon name="heart" item-right></ion-icon>
<button ion-button item-right color="danger" (click)="buttonClick($event)">Button</button>
</ion-item>
</ion-list>
</ion-content>
I got this issue in Console :
localhost/:1 XMLHttpRequest cannot load http://api.randomuser.me/?results=10. Redirect from 'http://api.randomuser.me/?results=10' to 'https://api.randomuser.me/?results=10' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.
Any help is appreciated.
I referred this Allow Control Allow origin for chrome extension. Then I followed this below steps:
Search Allow origin allow control in chrome web store and add it to browser.
Then turn on enable cross origin resource.
I think this will work:
this.http.get("http://api.randomuser.me/?results=10")
.map(res => res.json())
.subscribe(data => {
this.items = data;
console.log("Data is:",data,this.items);
},error=>{
console.log(error);// Error getting the data
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With