gpt4 book ai didi

java - 错误无法加载资源: the server responded with a status of 403 () en angular

转载 作者:太空宇宙 更新时间:2023-11-04 09:15:58 25 4
gpt4 key购买 nike

Errors I get

错误:

Failed to load resource: the server responded with a status of 403 ()
login:1 Access to XMLHttpRequest at 'http://localhost:8080/login' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
core.js:7187 ERROR Error: Uncaught (in promise): HttpErrorResponse: {"headers":{"normalizedNames":{},"lazyUpdate":null,"headers":{}},"status":0,"statusText":"Unknown Error","url":"http://localhost:8080/login","ok":false,"name":"HttpErrorResponse","message":"Http failure response for http://localhost:8080/login: 0 Unknown Error","error":{"isTrusted":true}}
at resolvePromise (zone-evergreen.js:797)
at zone-evergreen.js:707
at SafeSubscriber._error (Observable.js:91)
at SafeSubscriber.__tryOrUnsub (Subscriber.js:183)
at SafeSubscriber.error (Subscriber.js:135)
at Subscriber._error (Subscriber.js:75)
at Subscriber.error (Subscriber.js:55)
at MapSubscriber._error (Subscriber.js:75)
at MapSubscriber.error (Subscriber.js:55)
at FilterSubscriber._error (Subscriber.js:75)
defaultErrorLogger @ core.js:7187

JAVA spring boot中的POST服务(微服务的url:http:localhost:8080/login):

@CrossOrigin(origins = "*")
@RestController
public class dmoController {

@SuppressWarnings("rawtypes")
@PostMapping("/login")
public ResponseEntity<?> login(@RequestBody String data) {
System.out.print("entra");
System.out.print(data);
HttpHeaders headers = new HttpHeaders();
headers.add("Access-Control-Allow-Origin", "*");
headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
headers.add("Access-Control-Allow-Headers", "X-Requested-With,content-type");
headers.add("Access-Control-Allow-Credentials", "true");
return new ResponseEntity<>(headers, HttpStatus.CREATED);
}


}

从 Angular 调用帖子( Angular URL:http:localhost:4200/login):

  public async login(email: string, password: string) {
const data = { username: 'pepa', password: 'manoli' };

httpOptions.headers.append('Access-Control-Allow-Origin', '*');
httpOptions.headers.append(
'Access-Control-Allow-Methods',
'GET, POST, OPTIONS, PUT, PATCH, DELETE',
);
httpOptions.headers.append(
'Access-Control-Allow-Headers',
'X-Requested-With,content-type',
);
httpOptions.headers.append('Access-Control-Allow-Credentials', 'true');

this.http.post(this.url, JSON.stringify(data), httpOptions).toPromise();
}

最佳答案

解析 Cors 不是客户端的事情,因为它是您的本地主机开发,请求来源会有所不同。您需要配置服务器,使其允许来自所有来源的请求。

看看这个链接 https://spring.io/guides/gs/rest-service-cors/您可能会发现一些见解。

关于java - 错误无法加载资源: the server responded with a status of 403 () en angular,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58970698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com