对php API的Angular 4 POST调用失败,但GET请求工作和POST请求在Postman中工作

时间:2018-02-17 17:50:29

标签: php postman angular-http

我有一个连接到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 Object {_body:error,status:0,ok:false,statusText:“”,   标题:{...},输入:3,url:null}

     

来自原点的请求已被Cross-Origin阻止加载   资源共享策略:没有'Access-Control-Allow-Origin'标头   出现在所请求的资源上。

我已经用Postman测试了API,一切正常。

以下是DataService的代码

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());
}

我添加了ContentType标头,因为我发布了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;");

0 个答案:

没有答案
相关问题