I've actually crate an angular service to get data from the server which running on 'localhost:8080/' (endpoint /user) . But when I serve the angular project on 'localhost:4200' it made this Error :Access to XMLHttpRequest at 'localhost:8080/user' from origin ..
I tried to solve this problem by adding @CrossOrigin(origins = "*") in the controller and I add a proxy.conf.json in the angular project like I found here : 'https://angular.io/guide/build' But the problem persist .
this is the angular service :
@Injectable({
providedIn: 'root'
})
export class RestService {
api = 'localhost:8080/user'
constructor(private http: HttpClient) { }
getUsers(): Observable<String> {
return this.http.get<String>(this.api);
}
}
this is my proxy.conf.json :
{
"/*": {
"target": "http://localhost:8080",
"secure": false,
"logLevel": "debug"
}
}
this is the conf in angular.json :
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "crud:build",
"proxyConfig": "src/proxy.conf.json"
}
and this is the spring boot controller :
@RestController
@CrossOrigin(origins = "*")
public class userController {
@GetMapping("/user")
public String getuser(){
return "the user name is bilel" ;
}
}
"/*"is supported. But it is for your interest to prefix all the backend calls with/apior/backend, then you could use/apiin your proxy conf (it will make things easier for your Apache/Nginx conf later). Also, you must chose, either you handle this on frontend side (with the proxy, as you did) or from the backend side, with the@CrossOriginbut not both. IMHO,@CrossOrigin(origins = "*")is a very bad idea (absolute anti-pattern). Also, can you try to addchangeOrigin:truein your proxy conf ?