如何使用Angular HTTP代替Ionic Native HTTP

时间:2018-10-03 16:39:39

标签: angular ionic-framework ionic3

我正在将付款网关页面集成到Ionic 3应用程序中。我成功使用ionic native http集成了它,但问题是它有时有效,有时却无效。我听说有角度的http效果更好。

任何人都可以在下面的代码中进行更改,并告诉我如何修复它才能工作。

import {Component} from '@angular/core';
import Instamojo from 'instamojo-nodejs';
import {InAppBrowser} from '@ionic-native/in-app-browser';
import {HTTP} from '@ionic-native/http';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import 'rxjs/add/operator/map';
import { LoadingController } from 'ionic-    angular/components/loading/loading-controller';
import { ContactusPage } from '../contactus/contactus';
//import { ProfilePage } from '../profile/profile';

@Component({
  selector: 'page-new-transaction',
  templateUrl: 'new_transaction.html'
})
export class NewTransactionPage {
  amount;
  instamojoClient;

  constructor(public navCtrl: NavController, public navParams: NavParams, private iab: InAppBrowser, private http: HTTP,public loadingCtrl:LoadingController) {
    this.instamojoClient = new Instamojo(http, iab, 'http://xxxxx.com/access_token.php');
  }

  contactNow() {
    this
    .navCtrl    
    .push(ContactusPage);
  }

  payNow() {
    let loading = this.loadingCtrl.create({
      spinner: 'hide',
      content: `
        <div class="custom-spinner-container">
          <div class="custom-spinner-box"><img src="assets/imgs/loading.gif"></div>
          </div>`,
      duration: 5000
    });
    var data = this.instamojoClient.getPaymentFields();
    data.purpose = "Account";            // REQUIRED
    data.amount = 750;                  // REQUIRED
    // do not change this
    data.redirect_url = "http://localhost";
    loading.present().then(res=>{
      this.instamojoClient.payNow(data).then(response => {
        // alert("Payment complete: " + JSON.stringify(response));
        loading.dismiss();
      }).catch(err => {
        loading.dismiss();
        // alert("Payment failed: " + JSON.stringify(err));
        throw err;
      });
      //call the Safari View Controller

      // end of safari view controller

    })


  }



 }

1 个答案:

答案 0 :(得分:0)

您应该检查错误响应并从那里解决问题。
您遇到的问题可能与REST-API / Webserver / Whatever_you_are_using有关,而不是与Http请求的实现有关。
您可以检查https://ionicframework.com/docs/native/http/来检查服务器的答案,和/或对此采取一些措施。

this.httpIonicNativeCtrl.get(url + "?getParam1=" + this.selectedDate, {}, {})
 .then(data => {
   //data.data is where the answer is
   if (!data.data) {
    alert("Server doesn't answer with data");
    return;
   }
 })
 .catch(error => {
  console.log(error);
  alert("Error getting answer");
 })
;

使http响应正确,数字为200,您有数据答案等。
确保设备也具有活动的互联网连接。