简体   繁体   中英

Access Api service using Ionic2 provider

Can you tell me why I cannot access API service? It is working fine when I type it either on the browser or Postman.Why is it not working with ionic2 provider?

Provider:

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';

@Injectable()
export class EventData {
  data: any;

  constructor(public http: Http) {

  }

  getEventList(): any {
    if (this.data) {
      return Promise.resolve(this.data);
    }

    return new Promise(resolve => {
      this.http.get('https://mylink.com/admin/index.php?route=api/event')
        .map(res => res.json())
        .subscribe(data => {
          this.data = data;
          resolve(this.data);
        });
    });
  }

}

Exception:

error_handler.js:54 EXCEPTION: Response with status: 302 Found for URL: https://www.mylink.com/admin/index.php?route=api/event ErrorHandler.handleError @ error_handler.js:54 IonicErrorHandler.handleError @ ionic-error-handler.js:58 next @ application_ref.js:348 schedulerFn @ async.js:93 SafeSubscriber.__tryOrUnsub @ Subscriber.js:223 SafeSubscriber.next @ Subscriber.js:172 Subscriber._next @ Subscriber.js:125 Subscriber.next @ Subscriber.js:89 Subject.next @ Subject.js:55 EventEmitter.emit @ async.js:79 NgZone.triggerError @ ng_zone.js:333 onHandleError @ ng_zone.js:294 t.handleError @ polyfills.js:3 e.runTask @ polyfills.js:3 invoke @ polyfills.js:3

Update:

 getEventList(): any {
    return new Promise(resolve => {
      this.http.get('https://mylink.com/admin/index.php?route=api/event')
        .map(res => {
          console.log(res.headers.get('Location'));
          console.log(JSON.stringify(res.headers,null,2));
          return res.json();
        })
        .subscribe(data => {
          this.data = data;
          resolve(this.data);
        });
    });
  }

Heder detail

在此处输入图片说明

Postman Headers:

Cache-Control →no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection →Keep-Alive
Content-Length →2811
Content-Type →application/json
Date →Tue, 14 Mar 2017 02:31:50 GMT
Expires →Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive →timeout=5, max=100
Pragma →no-cache
Server →Apache
X-Powered-By →PHP/5.6.26

Response 302 is a redirect to a different URL. For more explanation regarding the response itself check this question and also this . In case of browsers, they handle the redirection directly. For your response if it is not possible to alter in the server side, you could get the redirect location in response.headers.location .

 return new Promise(resolve => {
      this.http.get('https://mylink.com/admin/index.php?route=api/event')
        .map(res => {console.log(res.headers.get('Location'));return res.json();})
        .subscribe(data => {
          this.data = data;
          resolve(this.data);
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM