【问题标题】:Redirect HTTP REQUEST FROM SPRING TO ANGULAR(10) WITH VALUES使用值将 HTTP 请求从 SPRING 重定向到 ANGULAR(10)
【发布时间】:2021-04-28 14:36:44
【问题描述】:

我的服务器后端是通过 HTTP GET REQUEST 从客户端 X(可点击链接)调用的,此客户端将值作为 RequestHeader 发送给我,我需要获取这些值(标题)并将其发送到我的前端(角度)最重要的是获得这些值并继续我的工作流程。

下图解释了我的工作流程

这是我的 java 代码(弹簧控制器)

private static final String NG_REDIRECT_URL= "http://localhost:4200";


@GetMapping(value = "/server-core")
    public RedirectView requestHandler(HttpServletRequest request, HttpServletResponse response,
            @RequestHeader(value = "user_id", required = true) String userId,
            @RequestHeader(value = "login", required = true) String login) throws IOException {
        return new RedirectView(NG_REDIRECT_URL, true);
    }
    

【问题讨论】:

  • 如果您需要通过重定向发送任何内容,则必须在 url 中使用查询参数..
  • 类似localhost:4200/list?login=login_value&user_id=user_value ????如果我提供的示例是正确的,如何从 Angular 应用程序中截取这些参数。谢谢你
  • 您可以从 ActivatedRoute 快照中读取这些值。reactgo.com/angular-get-query-params
  • 感谢您的回复,但我在 app.component.ts(根组件)的 onInit 方法中尝试了 ActivatedRoute,我得到了未定义的值
  • 你正在导航到 /list 所以我猜有一个列表组件?那是您可以访问它的地方..

标签: angular spring-boot rest http spring-mvc


【解决方案1】:

您需要在 angular 的 url 中添加值作为查询参数,并且您需要在 app.component.ts 的帮助下获取这些参数并使用这些值进行操作

ngOnInit() {
     const url = window.location.href;
     if (url.includes('?')) {
        const httpParams = new HttpParams({ fromString: url.split('?')[1] });
        const token = httpParams.get("token");
        //Here you will have you operations and you navigation with the parameters
        //this.router.navigate(['/home']);
     } else {
        //Here you will have your actual redirection
     }
}

并避免在路由模块(例如app.routing-module.ts)中使用空路径重定向。尝试手动导航到app.component.ts中的页面

我想这会对你有所帮助...

【讨论】:

    猜你喜欢
    • 2015-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-30
    • 2014-10-18
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    相关资源
    最近更新 更多