Ich rufe eine einfache get Anfrage über localhost.Ich weiß nicht, was falsch mit dem Code.Ich habe es so viel Zeit überprüft.Es hat keinen Fehler gefunden.Someone überprüfen Sie meinen Code sagen, was ich vermisse ich.Basic angular 2 Authentifizierung
import { Injectable } from '@angular/core';
import { Http, Response,Headers,RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import 'rxjs/add/observable/throw';
import { IProduct } from './product';
@Injectable()
export class ProductService {
//private _productUrl = 'api/products/products.json';
//private _productUrl = 'http://localhost:8092/openmrs-standalone/ws/rest/v1/concept';
private _productUrl = 'http://localhost:8092/openmrs-standalone/ws/rest/v1/session';
constructor(private _http: Http) { }
getProducts(): Observable<any> {
let username: string = 'admin';
let password: string = 'Admin123';
let headers: Headers = new Headers();
headers.append("authorization", "Basic " + btoa(username + ":" + password));
headers.append("Content-Type", "application/json");
let options = new RequestOptions({ headers: headers, withCredentials: true });
return this._http.get(this._productUrl,options)
.map((response: Response) => <IProduct[]> response.json())
.do(data => console.log('All: ' + JSON.stringify(data)))
.catch(this.handleError);
}
private handleError(error: Response) {
// in a real world app, we may send the server to some remote logging infrastructure
// instead of just logging it to the console
console.error(error);
return Observable.throw(error.json().error || 'Server error');
}
}
Warum Funktion getProducts() -Aufruf Auth API? –
Ich mache es gerade zu Testzwecken.Ich möchte den Rest api anrufen, um die Daten zurück zu bekommen.Newbie zu eckig 2 –
Ich postete den kompletten Code jetzt. –