I have a .net backback which works perfectly. But when I'm going to connect it with angular front I had this issue. Backend all request are post requests. Need to pass an ApiKey in the body of each request. With postman it works perfect.
Error:
PostMan:
resetService.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { map, catchError, tap } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class RestService {
httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
apiKey = {
'ApiKey': 'MTIzNDU2Nrg='
};
constructor(private http: HttpClient) {
console.log(this.httpOptions);
}
getProductCategories(): Observable<any> {
return this.http.post<any>('http://restUrl:8029/ShoppingCartApi/GetProductList', this.apiKey, this.httpOptions);
}
}
soap.component.ts
import { Component, OnInit } from '@angular/core';
import { RestService } from 'src/app/services/rest.service';
import { ActivatedRoute, Router } from '@angular/router';
@Component({
selector: 'app-soap',
templateUrl: './soap.component.html',
styleUrls: ['./soap.component.css']
})
export class SoapComponent implements OnInit {
products: any = [];
constructor(public rest: RestService, private route: ActivatedRoute, private router: Router) { }
ngOnInit() {
this.getProducts();
}
getProducts() {
this.products = [];
this.rest.getProductCategories().subscribe((data) => {
console.log(data);
this.products = data;
});
}
}
Any help is appreciated....