Angular 2 服务未从 HTTP 响应返回 JSON
Angular 2 Service Not Returning JSON from HTTP Response
我正在尝试 return JSON 来自 Web api 的数据,该服务可以很好地收集这些数据,当您将其输出到控制台时它可以正常工作并且 return 是我所期望的,但是当我尝试 return 将数据发送到服务之外的某个地方时,我只能 'undefined' 没有错误地返回。
服务方式
// Dashboard API Services
getModules() {
this._http.request(this._baseUrl + "Modules/Get").subscribe((res: Response) => {
this.modules = res.json();
});
return this.modules;
}
服务调用(在组件中)
import { Component, OnInit } from '@angular/core';
import { KoapiService } from '../koapi.service';
import { Http } from "@angular/http";
@Component({
selector: 'app-nav',
templateUrl: './nav.component.html',
styleUrls: ['./nav.component.css']
})
export class NavComponent implements OnInit {
modules: any;
constructor(private _koapi: KoapiService) { }
ngOnInit() {
this.modules = this._koapi.getModules();
console.log(this.modules);
}
}
在这里找到了一篇关于更好的方法的好文章:https://hassantariqblog.wordpress.com/2016/12/03/angular2-http-simple-get-using-promises-in-angular-2-application/
将我的代码更改为以下内容,这意味着服务现在可以从服务调用而不是服务内部获取任何 URL:
服务方式:
// On successful API call
private extractData(res: Response) {
let body = res.json();
return body || {};
}
// On Erronious API Call
private handleError(error: any): Promise<any> {
console.error('An error occurred', error);
return Promise.reject(error.message || error);
}
// Basic Get
getService(url: string): Promise<any> {
return this._http
.get(this._baseUrl + url)
.toPromise()
.then(this.extractData)
.catch(this.handleError);
}
服务电话:
// Get Enabled Modules
this._koapi
.getService("Modules/Get")
.then((result) => {
this.modules = result;
console.log(this.modules);
})
.catch(error => console.log(error));
}
1。
const headers = new Headers({
'Content-Type': 'application/json',
'Cache-control': 'no-cache',
过期:'0',
编译指示:'no-cache'
});
const options = new RequestOptions({ headers: headers });
您必须将此 'options' 与 url 一起发送。
我正在尝试 return JSON 来自 Web api 的数据,该服务可以很好地收集这些数据,当您将其输出到控制台时它可以正常工作并且 return 是我所期望的,但是当我尝试 return 将数据发送到服务之外的某个地方时,我只能 'undefined' 没有错误地返回。
服务方式
// Dashboard API Services
getModules() {
this._http.request(this._baseUrl + "Modules/Get").subscribe((res: Response) => {
this.modules = res.json();
});
return this.modules;
}
服务调用(在组件中)
import { Component, OnInit } from '@angular/core';
import { KoapiService } from '../koapi.service';
import { Http } from "@angular/http";
@Component({
selector: 'app-nav',
templateUrl: './nav.component.html',
styleUrls: ['./nav.component.css']
})
export class NavComponent implements OnInit {
modules: any;
constructor(private _koapi: KoapiService) { }
ngOnInit() {
this.modules = this._koapi.getModules();
console.log(this.modules);
}
}
在这里找到了一篇关于更好的方法的好文章:https://hassantariqblog.wordpress.com/2016/12/03/angular2-http-simple-get-using-promises-in-angular-2-application/
将我的代码更改为以下内容,这意味着服务现在可以从服务调用而不是服务内部获取任何 URL:
服务方式:
// On successful API call
private extractData(res: Response) {
let body = res.json();
return body || {};
}
// On Erronious API Call
private handleError(error: any): Promise<any> {
console.error('An error occurred', error);
return Promise.reject(error.message || error);
}
// Basic Get
getService(url: string): Promise<any> {
return this._http
.get(this._baseUrl + url)
.toPromise()
.then(this.extractData)
.catch(this.handleError);
}
服务电话:
// Get Enabled Modules
this._koapi
.getService("Modules/Get")
.then((result) => {
this.modules = result;
console.log(this.modules);
})
.catch(error => console.log(error));
}
1。 const headers = new Headers({ 'Content-Type': 'application/json', 'Cache-control': 'no-cache', 过期:'0', 编译指示:'no-cache' });
const options = new RequestOptions({ headers: headers });
您必须将此 'options' 与 url 一起发送。