One of the way to enable cross domain request on local chrome browser :
Now UI and API running on different ports will be able to work together. I hope this helps.
If you are looking for an example of Cross-domain request . I'll put it in fragments for you to get enough idea.
Angular Client
user.service.ts to call the SpringWebservice.
/** POST: Validates a user for login from Spring webservice */
loginUrl = 'http://localhost:8091/SpringWebService/login'; // URL to web api
validUser (user: User): Observable<User> {
return this.http.post<User>(this.loginUrl, user, httpOptions)
.pipe(
catchError(this.handleError('Login User', user))
);
}
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json;charset=utf-8',
'Authorization': 'my-auth-token'
})
};
login.component.html: to accept the user Name and pwd.
<form (ngSubmit)="onSubmit(loginForm)" #loginForm="ngForm">
<!-- //ngModel is a must for each form-control -->
<!-- 1st group -->
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name"
required name="name" ngModel #name="ngModel">
<div [hidden]="name.valid || name.pristine"
class="alert alert-danger">
Name is required
</div>
</div>
<!-- 2nd group -->
<div class="form-group">
<label for="pwd">Password</label>
<input type="text" class="form-control" id="pwd"
name="pwd" #pwd required ngModel>
</div>
<button type="submit" class="btn btn-success" [disabled]="!loginForm.valid">Submit</button>
</form>
login.component.ts: calls and subscribes validUser method of user.service.
userModel : User;
onSubmit(loginForm : NgForm) {
this.submitted = true;
console.log("User : "+loginForm.value.name + " Valid :"+loginForm.valid)
this.userModel.loggedIn= false;
this.userModel=new
User(loginForm.value.name.trim(),loginForm.value.pwd.trim())
// Passing the userModel to Service method to invoke WebAPI
this.userService.validUser(this.userModel).subscribe(user=>
{
if(user.loggedIn == false){
console.log("Invalid User/PWD");
}
else{
this.userService.changeUser(this.userModel);
this.router.navigate(['/home']);
}
}
);
user.ts: model.
export class User {
constructor(
public name : String,
public pwd : String,
public email ?: String, //optional
public mobile ? : number,//""
public loggedIn : boolean = false
){ }
}
Spring Webservice.
package com.rest;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
// This annotation opens door for cross-domain(Cross-origin resource sharing (CORS)) from any host
@CrossOrigin(origins="*")
public class MainController {
@RequestMapping(value="/login", method=RequestMethod.POST)
public User validUser(@RequestBody User user){
BaseResponse response = new BaseResponse();
if(user.getName().equalsIgnoreCase("shaz") && user.getPwd().equals("pwd")){
user.setLoggedIn(true);
}
else{
user.setLoggedIn(false);
}
return user;
}
}
Now when you pass name as "shaz" and pwd as "pwd" in the form and hit submit,it gets validated from the SpringWebService and the loggedIn flag is set to true and the user entity is returned in response. Based on the loggedIn status from response,the user is redirected to home page or an error is thrown.
Login Page and Network Details
Note: I have not shared the complete setup and code
Refer this: https://shahbaazdesk.wordpress.com/2018/04/03/angular5-with-spring-webservice/