【问题标题】:I am getting error:(Cross-Origin Request Blocked) ionic 2 [duplicate]我收到错误:(跨源请求被阻止)离子 2 [重复]
【发布时间】:2017-12-19 14:46:43
【问题描述】:

我收到以下错误: 跨域请求被阻止:同源策略不允许读取位于http://safe-pay.co/safepay/public/api/user 的远程资源。 (原因:CORS 标头“Access-Control-Allow-Origin”缺失)。

我在下面给出的是我的代码。任何机构都可以帮助我解决这个问题。

import { Injectable } from "@angular/core";
import { Http, Response     ,Headers } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class UserService {

userUrl = "http://safe-pay.co/safepay/public/api/user";

constructor(public http: Http) {        
}

getCategories() {
    let jsonData = {"Data":{
        "action": "CATEGORIES",
        "lang": "AR"
        },
       "Request": {

        }
    } 


    var headers = new Headers();
    headers.append('Content-Type', 'application/json');

    return this.http.post(this.userUrl,jsonData,{  
    headers: headers }).map(res => res.json()).subscribe(data => {
       console.log("data: ",data");
    },
    (err)=> console.log("error: ",err);
    );
}

【问题讨论】:

  • 它看起来像一个 CORS 问题..所以你必须在你的 BACK END 上启用 CORS
  • 您不能使用客户端 JavaScript 请求此资源,因为远程方必须明确允许这样做 - 显然他们没有。

标签: angular ionic2 angular2-services


【解决方案1】:

这与您的代码无关。出于安全原因,您的浏览器正在阻止它。

如果您使用的是 chrome,只需安装此扩展程序即可测试您的应用 => https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en

【讨论】:

  • 它第一次工作,但后来我得到了同样的错误
猜你喜欢
  • 2022-01-11
  • 2021-07-30
  • 2020-07-18
  • 2015-06-08
  • 1970-01-01
  • 2015-05-05
  • 2016-11-24
  • 2020-11-03
  • 2016-04-20
相关资源
最近更新 更多