0

I have a problem that I don't seem to figure out. I want to send a http request from my

Angular client

export class UsersService {

  private baseUrl = 'http://localhost:8095/rest/users';


  createUser(user: Object): Observable<Object> {
    return this.http.post(`${this.baseUrl}` , user);
  }

  getUsers(): Observable<any> {
    return this.http.get(`${this.baseUrl}/all`);
  }

}

create user component.ts :

 save() {
this.userService.createUser(this.user)
  .subscribe(data => console.log(data), error => console.log(error));
this.user = new User();}

this is the SpringBoot backend

@RestController
@RequestMapping("/rest/users")
public class UsersResource {

    private UserRepository userRepository;

    public UsersResource(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @GetMapping("/all")
    public List<Users> getAll() {
        return userRepository.findAll();
    }
    
    @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public void create(@RequestBody Users users) {
        userRepository.save(users);
    } 
}

when i call http://localhost:4200/users i got a clean result with all users without any problem but when i want to add a user from here http://localhost:4200/adduser it show for me some issue by the way iam using CORS chrome extension

enter image description here i hope that someone help me for this isse. thanks

Jonathan JOhx
  • 5,784
  • 2
  • 17
  • 33
  • 2
    Possible duplicate of [Spring Boot Security CORS](https://stackoverflow.com/questions/40286549/spring-boot-security-cors) – dotconnor Nov 25 '18 at 17:33
  • Not sure how springboot works, but I know how REST works .. So.. As chrome is reporting, you're requesting localhost:8025 for an action using AJAX but your page was received from localhost:4200. This is considered CORS which is a bad thing. Look at why your request is going to server running at port 8025. You should be sending your AJAX requests to server@4200 and handling them on that server. – anu Nov 25 '18 at 17:54
  • Did you add the proxy.conf.json file to your Angular application? – Gigaxel Nov 25 '18 at 18:19

0 Answers0