I need to retrieve data from a URL after posting the username and password. However, I encounter an error when trying to get the token using the GET method. The error message is: : Response for preflight has invalid HTTP status code 405.
@Component({
selector: 'app-inv',
templateUrl: './inv.component.html',
styleUrls: ['./inv.component.css'],
encapsulation: ViewEncapsulation.None
})
export class InvComponent implements OnInit {
userName= 'tcmuser';
password= '1234pass';
token : string;
error: any;
invList: any;
constructor(private invService: InvService) { }
ngOnInit() {
this.invService.login(this.userName, this.password);
this.invList = this.invService.getInvList(this.token)
.subscribe(
invList => this.invList = invList,
error => this.error = error
);
}
}
@Injectable()
export class InvService {
public token: string;
constructor(private http: Http) {
this.token = localStorage.getItem('token');
this.http = http;
}
login(userName: string, password: string): Observable<any> {
let url = `http://tcmtestapi.azurewebsites.net/api/demo/token`;
let body = JSON.stringify({userName,password});
let headers = new Headers({ 'Content-Type': 'application/json; charset=UTF-8' });
let options = new RequestOptions({ headers: headers });
return this.http.post(url, body, options)
.map((res: any) => {
let data = res.json();
this.token = data.token;
return this.token;
});
}
getInvList(token) {
this.token = token;
let url = `http://tcmtestapi.azurewebsites.net/api/demo/inventory`;
let headers = new Headers({'Authorization': 'Bearer ' + this.token});
let options = new RequestOptions({ headers: headers });
return this.http.get(url,options)
.map((response: Response) => response.json())
}
}