簡體   English   中英

Ionic - HTTP POST 請求在 iOS 中不起作用,但在 Android 和 Web 中起作用

[英]Ionic - HTTP POST Request not working in iOS, but working in Android and Web

我正在嘗試使用 HttpClient 通過 Ionic 中的發布請求發送推送通知。 它在 Android 和 Web 中運行,但在 iOS 中顯示此錯誤:

{"headers":{"normalizedNames":{},"lazyUpdate":null,"headers":{}},"status":0,"statusText":"Unknown Error","url":"https://fcm.googleapis.com/fcm/send","ok":false,"name":"HttpErrorResponse","message":"Http failure response for https://fcm.googleapis.com/fcm/send: 0 Unknown Error","error":{"isTrusted":true}}

這是我的功能:

 import {HttpClient, HttpHeaders} from "@angular/common/http";

 ...

 public sendPost(){
    let url = 'https://fcm.googleapis.com/fcm/send';
    let appKEY = 'key=my_key';
    let body =  {
      "condition":"'topico' in topics",
      "notification" : {
        "title": "Test",
        "body": "Hello!!!",
        "sound": "default"
      }
    }
    let header: HttpHeaders = new HttpHeaders();
    header = header.set('Authorization', appKEY);
    header = header.append("Content-Type", "application/json");
    const options = {
      headers: header
    }
    this.http.post(url, body, options)
    .subscribe(data => {
      alert("Success!!");
      console.log(data);
    }, error => {
      alert("Error!");
      console.log(error);
    });
  }

我的信息

Ionic:

   Ionic CLI                     : 5.4.16
   Ionic Framework               : @ionic/angular 4.11.10
   @angular-devkit/build-angular : 0.900.6
   @angular-devkit/schematics    : 9.0.6
   @angular/cli                  : 9.0.6
   @ionic/angular-toolkit        : 2.0.0

Capacitor:

   Capacitor CLI   : 1.4.0
   @capacitor/core : 1.5.0

Cordova:

   Cordova CLI       : 9.0.0 (cordova-lib@9.0.1)
   Cordova Platforms : none
   Cordova Plugins   : no whitelisted plugins (0 plugins total)

Utility:

   cordova-res : 0.10.0
   native-run  : not installed

System:

   NodeJS : v12.16.1 (/usr/local/bin/node)
   npm    : 6.13.4
   OS     : macOS Catalina
   Xcode  : Xcode 11.3.1 Build version 11C504

這只發生在這個網址上,我已經用另外兩個簡單的網址(沒有標題)進行了測試,並且帖子成功了。

我已經解決了。

return new Promise((resolve, reject) => {
  this.http.setServerTrustMode('nocheck');
  this.http.setHeader('*', 'Access-Control-Allow-Origin' , '*');
  this.http.setHeader('*', 'Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT');
  this.http.setHeader('*', 'Accept','application/json');
  this.http.setHeader('*', 'content-type','application/json');
  this.http.setHeader('*', 'Authorization', 'key=your-key-here')
  
  this.http.setDataSerializer('json');
  this.http.post(url, data, {}).then(res =>{
    resolve(JSON.parse(res.data));
  })
  .catch(err =>{
    reject(err);
  });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM