Php 使用Ionic2提供程序访问Api服务
你能告诉我为什么我不能访问API服务吗?当我在浏览器或邮递员上键入它时,它工作正常。为什么它不能与ionic2提供程序一起工作 提供商:Php 使用Ionic2提供程序访问Api服务,php,angular,ionic-framework,ionic2,restful-url,Php,Angular,Ionic Framework,Ionic2,Restful Url,你能告诉我为什么我不能访问API服务吗?当我在浏览器或邮递员上键入它时,它工作正常。为什么它不能与ionic2提供程序一起工作 提供商: import { Injectable } from '@angular/core'; import { Http } from '@angular/http'; import 'rxjs/add/operator/map'; @Injectable() export class EventData { data: any; constructor
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);
});
});
}
}
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);
});
});
}
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
例外情况:
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);
});
});
}
}
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);
});
});
}
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
错误\u handler.js:54异常:找到状态为302的响应
网址:
ErrorHandler.handleError@error_handler.js:54
IonicErrorHandler.handleError@ionic error handler.js:58下一步@
application_ref.js:348 schedulerFn@async.js:93
SafeSubscriber.\uuu tryorunsubscriber@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.triggerror@ng_zone.js:333 onHandleError@
ng_zone.js:294 t.handleError@polyfills.js:3 e.runTask@
polyfills.js:3调用@polyfills.js:3
更新:
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);
});
});
}
}
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);
});
});
}
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
海德尔细节
邮递员标题:
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);
});
});
}
}
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);
});
});
}
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
响应302是指向不同URL的重定向。有关响应本身的更多解释,请检查并。 对于浏览器,它们直接处理重定向。 对于服务器端无法更改的响应,您可以在
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);
});
编译时错误:
属性“location”在类型“headers”
上不存在。你知道为什么吗?location应该是headers中的属性。让我编辑答案仍然是相同的错误。请查看更新部分的最终代码。你能为控制台.log(JSON.stringify)添加日志吗(res.headers,null,2))
?应该在那里..你在postman中也得到相同的标题吗?那么我不确定。请查看postman标题。在postman上,它显示json数据,尽管它有302错误。知道吗?@surajI认为它正在重定向并给出最终响应的标题。还没有在移动应用程序中看到类似的内容…最坏的情况下,你可以尝试使用i纳普浏览器?这是个奇怪的想法……我能在这里做什么?@suraj