Angular 8 - 无法调用 webapi
Angular 8 - Can not call webapi
我正在尝试使用 parameter.i 从 Web api 获取数据,但无法获取 data.no t 也显示错误消息。
仪表板组件
国家模型:国家= {
country_id: '',
country_name: '',
状态:假
}
国家:可观察;
constructor(private api: RestApiService, private router: Router) { }
ngOnInit() {
let currentUserName = localStorage.getItem('currentUserName');
console.log(currentUserName);
this.GetCountry(currentUserName);
}
GetCountry(currentUserName: string) {
this.Countries = this.api.getcountry(currentUserName);
console.log(this.Countries);
}
}
api.service
export class RestApiService {
Url = 'http://localhost:xxxx/Api/Example/';
header: any;
constructor(private http: HttpClient) {
const headerSettings: { [name: string]: string | string[]; } = {};
this.header = new HttpHeaders(headerSettings);
}
getcountry(currentUserName: string): Observable<Country[]> {
console.log(this.Url + 'GetCountry?Username=' + currentUserName);
return this.http.get<Country[]>(this.Url + 'GetCountry?Username=' + currentUserName);
}
WebApi
[Route("Api/Example/GetCountry")]
[HttpGet]
public IHttpActionResult GetCountry(string Username)
{
}
如果您希望它执行,您必须订阅该调用。见 HttpClient
this.api.getcountry(currentUserName).subscribe((data)=>{
console.log(this.Countries);
});
我正在尝试使用 parameter.i 从 Web api 获取数据,但无法获取 data.no t 也显示错误消息。
仪表板组件
国家模型:国家= { country_id: '', country_name: '', 状态:假 } 国家:可观察;
constructor(private api: RestApiService, private router: Router) { }
ngOnInit() {
let currentUserName = localStorage.getItem('currentUserName');
console.log(currentUserName);
this.GetCountry(currentUserName);
}
GetCountry(currentUserName: string) {
this.Countries = this.api.getcountry(currentUserName);
console.log(this.Countries);
}
}
api.service
export class RestApiService {
Url = 'http://localhost:xxxx/Api/Example/';
header: any;
constructor(private http: HttpClient) {
const headerSettings: { [name: string]: string | string[]; } = {};
this.header = new HttpHeaders(headerSettings);
}
getcountry(currentUserName: string): Observable<Country[]> {
console.log(this.Url + 'GetCountry?Username=' + currentUserName);
return this.http.get<Country[]>(this.Url + 'GetCountry?Username=' + currentUserName);
}
WebApi
[Route("Api/Example/GetCountry")]
[HttpGet]
public IHttpActionResult GetCountry(string Username)
{
}
如果您希望它执行,您必须订阅该调用。见 HttpClient
this.api.getcountry(currentUserName).subscribe((data)=>{
console.log(this.Countries);
});