2017-03-13 36 views
1

您能告诉我为什么我无法访问API服务吗?当我在浏览器或Postman上输入它时,它工作正常。为什么它不能与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); 
     }); 
    }); 
    } 

赫德细节

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

请看postman headers.On邮递员它显示json数据,即使它有302错误。任何想法? @suraj – Sampath

+0

我认为这是重定向并给出最终响应的标题。没有看到这样的移动应用程序的东西...最糟糕的情况下,你可以尝试使用inAppBrowser?它的一个奇怪的想法.. –

回答

2

响应302是一个重定向到一个不同的URL。有关答复本身的更多解释,请检查this question以及this。 在浏览器的情况下,他们直接处理重定向。 为了您的回应,如果无法在服务器端进行更改,您可以在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'.compile时间错误:'属性'位置'不存在类型'Headers''.do你知道为什么吗? – Sampath

+0

位置应该是一个属性在标题..我编辑答案 –

+0

仍然是相同error.please请参阅更新部分上的最终代码。 – Sampath