2017-03-13 1 views
1

APIサービスにアクセスできない理由を教えてください。私はそれをブラウザまたはPostman.Whenそれをionic2プロバイダで動作していないのいずれかに入力すると正常に動作していますか?Ionic2プロバイダを使用してAPIサービスにアクセス

プロバイダ:

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); 
     }); 
    }); 
    } 

} 

例外:

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

更新:

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詳細

enter image description here

ポストマンヘッダ:

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 
+0

郵便配達員にも同じヘッダーが表示されていますか?それからわか​​らない。 –

+0

郵便配達員のヘッダーを参照してください。郵便配達員には、302エラーがありますが、jsonデータが表示されます。 @suraj – Sampath

+0

最終的な応答のヘッダーをリダイレクトして与えていると思います。 Haventはモバイルアプリのためにこれを見ました...最悪の場合、inAppBrowserを使用してみることができますか?その奇妙なアイデア.. –

答えて

2

応答302は、別のURLへのリダイレクトです。レスポンス自体の詳細については、this questionthisを確認してください。 ブラウザの場合、リダイレクトは直接処理されます。 サーバー側で変更できない場合は、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); 
     }); 
+0

ここにこのエラーが出ます。 'res.headers.location'。ファイルの時刻エラー:' Property 'location'は 'Headers''.doに存在しませんなぜですか? – Sampath

+0

場所はヘッダーのプロパティである必要があります。私は答え –

+0

を編集してください。まだ同じエラーが表示されます。更新セクションの最終コードを参照してください。 – Sampath

関連する問題