【问题标题】:CORS Policy : No 'Access-Control-Allow-Origin' header is present on the requested resource in ionic 2CORS 策略:在 ionic 2 中请求的资源上不存在“Access-Control-Allow-Origin”标头
【发布时间】:2020-03-02 17:28:18
【问题描述】:

编辑:

我是混合开发的新手。我提到了这个Sample 来使用 ionic2 解析列表视图中的 json。但是当我运行代码时,我只能在浏览器中看到空白屏幕。

下面我已经发布了代码。请检查:

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>

我在 控制台 中遇到了这个问题:

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.

感谢任何帮助。

【问题讨论】:

  • 控制台有错误吗?
  • ionic serve的命令行是否有编译错误?
  • @suraj sorry。现在只有我知道控制台。以前我认为是命令提示符。我已经发布了控制台问题。
  • 这是一个cors问题,尝试设置代理..stackoverflow.com/a/37779476/4826457这个错误只发生在浏览器中..不在设备中

标签: typescript ionic2 angular2-routing hybrid-mobile-app


【解决方案1】:

我将这个 Allow Control Allow origin 用于 chrome 扩展。然后我按照以下步骤操作:

  • 在 chrome 浏览器中,设置 -> 扩展 -> 向下滚动并单击 获取更多扩展。
  • 在chrome网上商店搜索Allow origin允许控制并将其添加到浏览器中。

  • 然后开启开启启用跨源资源。

【讨论】:

  • CORS 是否仍不会阻止客户浏览器中的请求?
【解决方案2】:

我认为这会奏效:

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
    });

【讨论】:

  • 另外,请确保其他事项已到位。例如。 http.get 以正确的方式为您提供您想要的项目数组。另外,如果有任何 UI 错误。
猜你喜欢
  • 2017-09-10
  • 2021-08-29
  • 1970-01-01
  • 1970-01-01
  • 2020-01-24
  • 2021-09-29
  • 2021-12-27
  • 2014-07-06
  • 2017-04-11
相关资源
最近更新 更多