简体   繁体   English

CORS问题-Angular / PHP / Apache

[英]CORS Issue - Angular / PHP / Apache

I think that there are plenty of similar topics here and there on the internet, but I did just spend 1h searching and still can't fix this. 我认为互联网上到处都有很多类似的话题,但是我只是花了1个小时进行搜索,但仍然无法解决。

I cannot make a request with POST on my server (Apache & PHP) with Angular. 我无法使用Angular在服务器(Apache和PHP)上通过POST发出请求。

I use angular/cli v.6.2.1 with node 10, apache 2.4 & php 7.1 我在节点10,Apache 2.4和PHP 7.1中使用angular / cli v.6.2.1

Here is a simple code from the Http call (HttpClient & HttpHeaders both come from @angular/common/http) : 这是来自Http调用的简单代码(HttpClient和HttpHeaders都来自@ angular / common / http):

constructor(private http: HttpClient){}

this.http.post('http://localhost/distributor.php', ['prop1':'value1', 'prop2':'value2'], {headers: new HttpHeaders().set('Access-Control-Allow-Origin','*')}).subscribe(
data => {
    console.log(data);
},
err => {
    console.log('error');
});

} }

I just try to send something back from PHP this way : 我只是尝试以这种方式从PHP发送回一些东西:

<?php
    $data = $_POST;
    echo json_encode($data);

I already allowed all origins in apache configuration file. 我已经允许所有起源在apache配置文件中。 Both Firefox & Chrome just let me down after a "OPTIONS" preflight and do not do anything else, no return from the PHP file. Firefox和Chrome都只是在“ OPTIONS”预检后让我失望,并且不执行其他任何操作,也没有从PHP文件返回任何内容。

Here is what FireFox shows me : 这是FireFox向我展示的内容:

在此处输入图片说明

and I can see this in the network tab : 我可以在“网络”标签中看到以下内容:

在此处输入图片说明

Response tab shows a completely empty box. 响应选项卡显示一个完全空白的框。

I can remove the custom header's part from my http.post it changes nothing. 我可以从http.post中删除自定义标头的部分,它什么都不会改变。

What seems strange to me is that I can click the FireFox edit & resend button, without changing nothing, and the right results appear... 对我来说似乎很奇怪的是,我可以单击FireFox编辑并重新发送按钮,而无需进行任何更改即可显示正确的结果...

Thanks for reading/help 感谢您的阅读/帮助

First you need to fix your POST data; 首先,您需要修复POST数据; you have square brackets around Object syntax. 您在对象语法周围有方括号。

const data = { 'prop1': 'value1', 'prop2': 'value2' };
this.http.post('http://localhost/distributor.php', data).subscribe(
  reply => {
    console.log(reply);
  },
  err => {
    console.log('error', err);
  });

Next, you need to add proper headers and set PHP up to deal with JSON: 接下来,您需要添加适当的标头并设置PHP以处理JSON:

<?php
header('Access-Control-Allow-Headers: Access-Control-Allow-Origin, Content-Type');
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json, charset=utf-8');

// grab JSON data sent by Angular
$data = json_decode(file_get_contents('php://input'), true);
// add numeric data
$data["prop3"] = 3;
// reply
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_NUMERIC_CHECK);

This worked for me. 这对我有用。

I had the same issue but it resolved by adding these lines to your php code 我有同样的问题,但是通过将这些行添加到您的php代码中来解决了

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: X-Requested-With');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Content-Type: application/json');

let data = {
            'prop1': 'value1',
            'prop2': 'value2'
        };
        return this.http.post('http://localhost/distributor.php', data, {headers: new HttpHeaders().set('Access-Control-Allow-Origin', '*')}).map((response: Response) => {
            let data = response;
            if (data) {
                console.log(data);
            }
        })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM