我有一个连接到 PHP API 的 Angular 5 应用程序,在本地环境(Ubuntu 16.04、Apache2 2.4.18、PHP 7.2.2)中测试时一切正常,但是当我在学校服务器(Uubuntu、Apache2 2.4)中部署该项目时,一切正常.27,PHP 7.1.11),GET 请求正常,但 POST 请求会变成 OPTIONS 请求,并且 Angular 显示错误。
ERROR 对象 { _body:错误,状态:0,ok:错误,statusText:“”,
标题:{…},类型:3,url:null }
来自来源的请求已被跨来源阻止加载
资源共享策略:没有“Access-Control-Allow-Origin”标头
存在于所请求的资源上。
我已经用 Postman 测试了 API,一切正常。
这是数据服务的代码
constructor(public http:Http) {
this.url = 'http://10.50.67.83/usuario1/Autozone/';
this.headers = new Headers();
this.headers.append('Content-Type', 'application/json');
}
postRequisition(client:string, cart:Selected[]) {
let data:Data;
data = {client: client, cart: cart};
return this.http.post(
this.url + 'api/requisitions/create.php',
JSON.stringify(data),
{
method: 'POST',
headers: this.headers
}).map(res => res.json());
}
我添加了内容类型标头,因为我要发布 JSON。
这些是 php 文件中包含的标头。
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Allow-Headers: Content-Type");
header("Content-Type: application/json; charset=UTF-8");
header("Accept: application/json;");
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)