我得到错误:(跨源请求被阻止)ionic 2
问题描述:
我收到以下错误: 跨域请求被阻止:同源策略不允许读取http://safe-pay.co/safepay/public/api/user处的远程资源。 (原因:缺少CORS头'Access-Control-Allow-Origin')。我得到错误:(跨源请求被阻止)ionic 2
我在下面给出的是我的代码。任何机构都可以帮助我解决这个问题。
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);
);
}
答
它与您的代码无关。出于安全原因,您的浏览器正在阻止它。
如果您使用Chrome,只要安装这个扩展的应用测试=>https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en
+0
它第一次工作,但后来我得到了同样的错误 –
它看起来像一个CORS问题..所以你必须eanble你的后端CORS –
你不能要求这个资源使用客户端JavaScript,因为远程方必须明确地允许这一点 - 显然他们没有。 – CBroe