Cancel a Native Http Request


#1

Is there a way to cancel an http request made using the Cordova Native HTTP plugin. I have read that one can unsubscribe when using the Angular HTTP Module, but I can not figure out how this is done with the native plugin. I need to use the native plugin due to a CORS issue. I need to be able to cancel the request when the user leave the page. Currently, if the the user make the request and then leaves the page, the request will continue and return anyway. Please help.

Here is the request in my provider:

  getBiblePassagesAudio(searchTerm) {
    return new Promise(resolve => {
      let url = 'https://api.esv.org/v3/passage/audio/?q=' + searchTerm;
      this.http.get(url, {}, { Authorization: 'Token ' + this.esvToken }).then(data => {
        resolve(data.url);
      }).catch(_error => {
        resolve('error');
      });
    });
  }

Here is how I am calling it.

  async playAudio() {
    const loading = await this.loadingController.create({
      spinner: null,
      cssClass: 'custom-loading'
    });
    this.bibleService.getBiblePassagesAudio(this.title).then(data => {
      loading.dismiss();
      if (data !== 'error') {
        this.streamingMedia.playAudio(data.toString());
      } else {
        this.presentErrorAlert(2);
      }
    });
    return await loading.present();
  }