相关疑难解决方法(0)

Angular - 为每个请求设置标头

我需要在用户登录后为每个后续请求设置一些授权标头.


要为特定请求设置标头,

import {Headers} from 'angular2/http';
var headers = new Headers();
headers.append(headerName, value);

// HTTP POST using these headers
this.http.post(url, data, {
  headers: headers
})
// do something with the response
Run Code Online (Sandbox Code Playgroud)

参考

但是以这种方式为每个请求手动设置请求标头是不可行的.

如何在用户登录后设置标头集,并在注销时删除这些标头?

angular

315
推荐指数
13
解决办法
37万
查看次数

使用 Angular 在 Spring Boot 中启用 Cors,仍然出现 cors 错误

我为所有来源和标题启用了 cors,但是当我get从我的 angular 应用程序调用一个方法到 spring boot时,我仍然收到 cors 错误。

来自控制台的 Cors 错误:

Access to XMLHttpRequest at 'http://localhost:8080/api/users/test@ronny.nl' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Run Code Online (Sandbox Code Playgroud)

我的controller(我打电话给 getbyemail):

@CrossOrigin(origins = "*", allowedHeaders = "*")
@RestController
@RequestMapping(value = "/api/users", produces = MediaType.APPLICATION_JSON_VALUE)
public class UserController {

    private final UserService userService;

    @Autowired
    public UserController(final UserService userService) {
        this.userService = userService;
    }

    @GetMapping
    public List<UserDTO> getAllUsers() {
        return userService.findAll();
    }

    @GetMapping("/{id}") …
Run Code Online (Sandbox Code Playgroud)

cors spring-boot angular

7
推荐指数
2
解决办法
1万
查看次数

标签 统计

angular ×2

cors ×1

spring-boot ×1