离子 4 + Laravel CORS

IONIC 4 + Laravel CORS

好的,我已经在网上搜索了几个小时,找到了很多结果,但没有有效的:(

我正在用 ionic 4 开发一个应用程序。到目前为止,我一直在使用本地 "api" 进行测试(有效),但现在我想使用使用 [= 构建的实时版本对其进行测试43=] - 它是本地版本的精确副本,但存在于域中。

我已经安装了 https://github.com/barryvdh/laravel-cors to handle the cors and when i test the cors on https://www.test-cors.org 它显示:

date: Fri, 07 Jun 2019 22:18:39 GMT
content-encoding: gzip
server: Google Frontend
access-control-allow-origin: *
etag: "1rO4KQ"
allow: POST, GET, OPTIONS, PUT, DELETE
access-control-allow-methods: POST, GET, OPTIONS, PUT, DELETE
content-type: text/html
status: 200
x-cloud-trace-context: c463d031b08229c22feccaba743c28a5
cache-control: public, max-age=600
access-control-allow-headers: access-control-allow-headers,access-control-allow-methods,access-control-allow-origin,content-type
expires: Fri, 07 Jun 2019 22:28:39 GMT

所以我想我的服务器端 cors 没问题。现在在 ionic im 中执行以下操作:

  getPartnersForZipcode(zipcode: string): Observable<any> {
return this.http.post(this.baseUrl+'/search-partners', {zipcode:zipcode}, this.httpOptions).pipe(
    map(results => results),
);

}

作为我使用的 http 选项

  httpOptions = {
headers: new HttpHeaders({
  'Content-Type': 'application/json',
  'Access-Control-Allow-Origin':'*',
  'Access-Control-Allow-Methods':'POST, GET, PUT, OPTIONS, DELETE, PATCH',
  'Access-Control-Allow-Headers': 'Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With',
})

}; *我也尝试过没有 headers,也只有内容类型等。none 有所不同

...我不知道发生了什么...为什么我的请求在 ionic 中不起作用?

哪个正在使用这个包 import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';

在邮递员中,我得到了很好的数据响应

但在 ionic 中(在设备上使用 ionic --serve 和 devapp (ios))我收到 CORS 错误

好的,我将此发布给有同样问题的用户。

出于某种原因,使用 header 'Content-Type': 'application/json'(默认设置)的 ionic 中的 POST 将导致此错误。

我已将其更改为

  httpOptions = {
headers: new HttpHeaders({
  // 'Content-Type': 'application/json',
  'Content-Type': 'application/x-www-form-urlencoded',
})

};

现在我从网络服务器取回数据。这不是最好的解决方案,但现在可以了。