我正在尝试从数据库中获取数据。 我可以登录,但无法从数据库中检索数据。 但是,通过Postman进行测试会返回状态200。
这是我的API调用索引:
export let CONTEXTROOT="http://localhost:8080/core/api/";
这是我的getDashboardData()函数:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable,pipe } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { map } from 'rxjs/operators';
import { CONTEXTROOT } from '../contextRoot';
// import 'rxjs/add/operator/do'; // for debugging
/**
* This class provides the Data service with methods to get and post data.
*/
@Injectable()
export class DataService {
/**
* Creates a new DataService with the injected Http.
* @param {Http} http - The injected Http.
* @constructor
*/
constructor(private http: HttpClient) {}
/**
* Returns an Observable for the HTTP GET request for the JSON resource.
* @return {string[]} The Observable for the HTTP request.
*/
getDashBoardData(): Observable<any> {
return this.http.get(CONTEXTROOT+'dashboard')
// .do(data => console.log('server data:', data)) // debug
.pipe(catchError(this.handleError));
}
这是我的错误处理功能:
private handleError (error: any) {
// In a real world app, we might use a remote logging infrastructure
// We'd also dig deeper into the error to get a better message
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg); // log to console instead
return Observable.throw(errMsg);
}
}
为什么我不能启用“获取数据”?我也有启用Chrome的CORS插件。