Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CORS Policy : No 'Access-Control-Allow-Origin' header is present on the requested resource in ionic 2

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.

like image 962
Steve Avatar asked Jan 04 '23 02:01

Steve


2 Answers

I referred this Allow Control Allow origin for chrome extension. Then I followed this below steps:

  • In chrome browser, Settings -> Extensions -> scroll down and click Get More Extensions.
  • Search Allow origin allow control in chrome web store and add it to browser.

    enter image description here

  • Then turn on enable cross origin resource.

    enter image description here

like image 102
Steve Avatar answered Jan 21 '23 14:01

Steve


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
    });
like image 26
Sagar Kulkarni Avatar answered Jan 21 '23 13:01

Sagar Kulkarni