我有一個角度4的客戶服務,調用一個休息方法在一個Java服務器上用Postmapping註釋聲明。通過角度4 http post連接到b a java rest api不允許在服務器端
當我從角度調用它時,它不被服務器接受。但在郵遞員上試用它時,它僅適用於添加頭Content-Type application/json。
添加了相同的頭文件,甚至還有一些頭文件仍然無法使用角度。
這裏對角服務代碼:
public login(username: string, password: string): Observable<{}> {
const requestParam = {
username: username,
password: password,
email: '[email protected]'
};
const options = this.generateOptions();
const body = JSON.stringify(requestParam);
return this.http
.post(AuthService.SIGNIN_URL, body, options)
.map(this.extractData)
.catch(this.handleError);
}
的頭我都不得不時刻:
const headers = new Headers({
'Access-Control-Allow-Origin' : '*',
'Access-Control-Allow-Methods': 'GET, POST, PATCH, PUT,
DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Origin, Content-Type, X-
Auth-Token',
'Content-Type': 'application/json'
});
而且在瀏覽器上給定的錯誤:
2zone.js:2744 OPTIONS
http://localhost:8080/api/auth/login net::ERR_ABORTED
invokeTask @ zone.js:1427 globalZoneAwareCallback @
zone.js:1445 login?returnUrl=%2F:1 Failed to load
http://localhost:8080/api/auth/login: Response to
preflight request doesn't pass access control check: No
'Access-Control-Allow-Origin' header is present on the
requested resource. Origin 'http://localhost:4200' is
therefore not allowed access. The response had HTTP
status code 403. auth.service.ts:205 Server error
服務器方正在使用彈簧靴:
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
/**
* Returns authentication token for given user
*
* @param authenticationRequest
* with username and password
* @return generated JWT
* @throws AuthenticationException
*/
@PostMapping(LOGIN_URL)
public ResponseEntity getAuthenticationToken(@RequestBody JwtAuthenticationRequest authenticationRequest)
throws AuthenticationException {
System.out.println("on login server side");
...
}
public class JwtAuthenticationRequest implements Serializable {
private String username;
private String email;
private String password;
...
}
謝謝!
服務器端是否使用彈簧?你能爲我們分享你的服務器端嗎? –
爲服務器端添加了詳細信息。 –