简体   繁体   English

HTTP API调用PHP后端的SPDY_PROTOCOL错误

[英]SPDY_PROTOCOL error on HTTP API call to PHP backend

I have an Ionic 4 app with a PHP + mysql db backend. 我有一个带有PHP + mysql数据库后端的Ionic 4应用程序。 I have developed and tested both locally using XAMPP. 我使用XAMPP在本地开发和测试。 Recently, I have migrated my backend to a webhost. 最近,我已将我的后端迁移到webhost。 When testing via POSTMAN, the call finishes correctly. 通过POSTMAN进行测试时,呼叫正确完成。 When, however, creating an HTTP call in Ionic, I always get an error. 但是,当在Ionic中创建HTTP调用时,我总是会收到错误。 In chrome, it looks like this: 在chrome中,它看起来像这样: SPDY_ERR Chrome

In IE, the exact same error occurs. 在IE中,发生完全相同的错误。

My PHP backend has CORS enabled using the following lines: 我的PHP后端使用以下行启用了CORS:

Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type, authorization"
Header add Access-Control-Allow-Methods: "GET,POST,OPTIONS,DELETE,PUT"

The call in Angular looking like this: Angular中的调用看起来像这样:

let apiUrl = 'https://xxxxxxxx.000webhostapp.com/index.php/';

@Injectable({
  providedIn: 'root'
})
export class ApiService {

  constructor(public http: HttpClient) { }

  postData(data, type) {
    return new Promise((resolve, reject) => {
      const httpOptions = {
        headers: new HttpHeaders({
          'Content-Type': 'application/json'
        })
      };

      this.http.post(apiUrl + type, JSON.stringify(data), httpOptions).subscribe(res => {
        resolve(res);
      }, (e) => {
        reject(e);
      });
    });
  }

  ...

Finally, the Headers of the failed request in Chrome, if useful: 最后,Chrome中失败请求的标头,如果有用的话:

在此输入图像描述

Some google searchings taught me that this is most likely a CORS related error. 一些谷歌搜索告诉我,这很可能是与CORS相关的错误。 I find it weird however that, using POSTMAN, the call works perfectly but via the app it does not, even though I use the same headers, URL and body. 我觉得很奇怪但是,使用POSTMAN,调用工作完美,但通过应用它不会,即使我使用相同的标题,URL和正文。

Any help is highly appreciated. 任何帮助都非常感谢。

Thanks 谢谢

To anyone encountering this problem, I fixed this by switching hosts. 对于遇到此问题的任何人,我通过切换主机来修复此问题。 As you can see in the OP, I had everything set up at 000webhost. 正如你在OP中看到的那样,我在000webhost上设置了所有内容。 A paid host with the same configuration as above fixed it for me. 与上述配置相同的付费主机为我修复了它。 They have a different approach when it comes to generating SSL certificates, and I just assume that was the cause. 在生成SSL证书时,他们有不同的方法,我只是假设这是原因。

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

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