gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 20:54:16 25 4
gpt4 key购买 nike

编辑:

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

下面我贴出了代码。请检查:

页面.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>

我在 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.

感谢任何帮助。

最佳答案

我提到了这个Allow Control Allow origin用于 Chrome 扩展。然后我按照以下步骤操作:

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

    enter image description here

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

    enter image description here

关于typescript - CORS 策略 : No 'Access-Control-Allow-Origin' header is present on the requested resource in ionic 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43654369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com