2018年6月11日月曜日

ionic3 で HTTP 通信

基本的なことは下記のページに書かれています。


$ ionic cordova plugin add cordova-plugin-advanced-http
$ npm install --save @ionic-native/http

これで準備は完了。

let url = 'http://hoge.hoge.com/test.php';

let body = {
  'sid': this.data.server.siteID,
  'tac': this.data.terminal.account,
  'tpw': this.data.terminal.password,
};

let headers = {};

this.http.post(url, body, headers).then((res: HTTPResponse) => {
  // 成功
  console.log(res);
  resolve(res);
}).catch((res: any) => {
  // エラーが発生した
  console.log(res);
  resolve(res);
});

利用も簡単!

なんて楽ちんなんだろうと思ったら大間違いでした…

Android ではなんの問題もなく通信できたのですが、iOS では then() にも catch() にも来ません???

XCode のコンソール出力を見ると、cordovaHTTP is not defined と出力されています???
なんでだろうと散々調べたのですがさっぱりわからず。念の為 cordova-plugin-advanced-http をremove 後に、再度 add。
cordovaHTTP is not defined は出なくなり、http.post() 呼び出し後リクエストの送信は行われるのですが、相変わらず then() にも catch() にも来ません><;

先に種明かしすると、この原因は content-type でした。

クライアントからのリクエストもサーバーからのレザルトも単なるテキストなので、plain/text を返していたのですが、application/json を返さないとサーバーからのレザルトを受け取らないようです。

いやはやマイナーOSは情報少ない上に癖が強くてホント苦労させられる><;

0 件のコメント: