我收到以下错误: 跨源请求已阻止:同源策略禁止在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);
);
}
答案 0 :(得分:0)
它与您的代码无关。出于安全原因,您的浏览器会阻止它。
如果您正在使用Chrome,只需安装此扩展程序即可测试您的应用=> https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en