I'm trying to connect the frontend of an app made in Angular to a backend in Java. I'm trying to pass the data through an API and every time I try to do a post I get the following error:
missing header CORS "access-control-origin" status code: 403
Here's the code of the page I'm trying to do the post from:
import { Component, OnInit } from '@angular/core';
import { NgForm } from '@angular/forms';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
name = "";
surname = "";
birthday: number = 0;
email = "";
nickname = "";
password = "";
pasDue!: string;
NameAlready!: string;
passAlready!: string;
constructor() { }
public onSubmit(form: NgForm){
this.name = form.value.FullName;
this.surname = form.value.Surname;
this.birthday = form.value.birthday;
this.email = form.value.eMail;
this.nickname = form.value.Nickname;
this.password = form.value.Password;
this.pasDue = form.value.passwordDue;
let nickname = this.nickname;
let birthday = this.birthday;
let email = this.email;
let name = this.name;
let surname = this.surname;
let password = this.password;
const url = `http://localhost:8080/api/v1/user?user_name=${this.nickname}&password=${this.password}&name=${this.name}&surname=${this.surname}&birth=${this.birthday}&e_mail=${this.email}`;
//console.log("url"+url);
try{
fetch(url, {
method: "POST",
mode: "cors",,
headers :{
"Content-Type":"application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({ nickname, birthday, email, name, surname, password }),
}).then((resp)=>{
resp.json();
}).then((data)=>{
console.log(data);
})}catch(err){
console.log(err);
}
}
public onSubmitNew(form: NgForm){
this.passAlready = form.value.passswordAlready;
this.NameAlready = form.value.eMail;
}
ngOnInit(): void {
}
}
I built the database and also the backend. What am I doing wrong? I heard that it can be a cross-domain issue. If so, how to solve it?
EDIT: After the suggestion of carloliwanag I share with you the code of the controller class in spring boot which handles the calls to the database:
package com.example.demo.User;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping(path = "api/v1/user")
public class UserController {
private final UserService userService;
@Autowired
public UserController(UserService userService) {
this.userService = userService;
}
@GetMapping
public List<UserOfPopster> getStudents(){
return userService.getStudents();
}
@PostMapping
public void registerNewUser(@RequestBody UserOfPopster user){
userService.addNewUser(user);
}
@DeleteMapping(path = "{user_id}")
public void deleteUser(@PathVariable("user_id") Long id){
userService.deleteUser(id);
}
@PutMapping(path = "{user_id}")
public void putUser(@PathVariable("user_id") Long userId, @RequestParam(required = false) String name, @RequestParam(required = false) String eMail){
userService.updateUser(userId, name, eMail);
}
}
So should I add the enableCors function here? Can you please give me some examples of how can I structure it? (Please, don't write the function for me, just give me an example since I never wrote any of these functions before).