Hallo zusammen,Ich habe vor mir über den Winter eine kleine Web App zu basteln, bin aber Anfänger.Habe mit Visual Studio 2022 ein Projekt mit der Vorlage ASP.NET Core mit Angular erstellt. Als Authentifizierungstyp habe ich Einzelne Konten gewählt.Nun habe ich Im Backend ein simples Modul für Produckte und ein Controller HttpGet..POST…Put..usw. gemacht. Das ganze habe ich mit Postman getestet und ich kann alle Aktionen ausführen.Dann hab ich in Angular eine Komponente (Lager) erstellt, einen Service , eine Component und eine Html.Nun versuche ich ein POST abzusetzen der mir die Daten an das backend sendet, doch wird er vorher in Angular schon abgefangen und ich bekomme POST localhost:7199/api/lager 400 (Bad Request) authorize.service.ts:63Ich weiß das es mit der authorize zu tun haben muss, doch schnalle ich noch nicht wie ich das hinbekommen soll.
Lger.component
lager.service
Code-Tags korrigiert, Topic verschoben. ~Thunderbolt
Lger.component
Quellcode
- import { Component } from '@angular/core';
- import { LagerService } from './lager.service';
- @Component({
- selector: 'app-lager',
- templateUrl: './lager.component.html',
- })
- export class LagerComponent {
- constructor(private lagerService: LagerService) { }
- datenHochladen(): void {
- const hartcodierteDaten = [
- { Name: 'Produkt 1', Menge: 10 }
- ];
- this.lagerService.datenHochladen(hartcodierteDaten).subscribe(
- response => {
- console.log('Daten erfolgreich hochgeladen:', response);
- },
- error => {
- console.error('Fehler beim Hochladen der Daten:', error);
- }
- );
- }
- }
lager.service
Quellcode
- import { Inject, Injectable } from '@angular/core';
- import { HttpClient, HttpHeaders } from '@angular/common/http';
- import { Observable } from 'rxjs';
- @Injectable({
- providedIn: 'root'
- })
- export class LagerService {
- constructor(private http: HttpClient, @Inject('BASE_URL') private baseUrl: string) { }
- public datenHochladen(daten: any): Observable<any> {
- const headers = new HttpHeaders().set('Content-Type', 'application/json');
- console.log('Daten werden hochgeladen:', { headers: headers });
- return this.http.post<any>('https://localhost:7199/api/lager', JSON.stringify(daten), { headers: headers })
- }
- }
Code-Tags korrigiert, Topic verschoben. ~Thunderbolt
Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „Marcus Gräfe“ ()