简体   繁体   English

如何从 angular 通过 springboot 重定向

[英]How do I redirect through springboot from angular

I have backend springboot that does the redirect我有执行重定向的后端 springboot

    @RequestMapping(value = "/test/page", method = RequestMethod.POST)
    public @ResponseBody void getPage(@RequestBody custRequest req, HttpServletRequest request, HttpServletResponse response) {
   response.sendRedirect("https:www.google.com");
}

And the service call like this和这样的服务电话

  getPage(user: User) {
        const bodyString = JSON.stringify(user);
        const header = new HttpHeaders({ 'Content-Type': 'application/JSON' ,
                                          'Accept' : 'text/html' });
        return this.httpclient.post('/test/page', bodyString, {headers: header, responseType: 'text'} );
  }

Button when clicked.单击时的按钮。

  onSubmit(user: User) {
    if (user && user.userID && user.password) {
      this.user.session = this.uuid;
      this.loginService.getLogin(user).subscribe(
        (res) => {
          console.log('post res');
          console.log(res);
        //  window.location.href = data.url; sanitizer
        this.data = this.sanitizer.sanitize(SecurityContext.HTML, res);
        },
        error => {
          console.log('Error', error);
            },
        () => {
          console.log('POST is completed');
        });

But in the result page I am not getting a proper landing page.但是在结果页面中,我没有得到正确的登录页面。 Instead I am staying in the same page and the console would print相反,我停留在同一页面,控制台会打印

<!doctype html>
<html lang="en">
<head>
...

</html>

My question is how do I show the actual content?我的问题是如何显示实际内容?

Simply put, your code fetches the source code of google.com.简而言之,您的代码获取 google.com 的源代码。 I'm not sure whether this is the behaviour you want, or you want just a browser redirect.我不确定这是您想要的行为,还是您只想要浏览器重定向。 Please explain in more detail what you're trying to achieve.请更详细地解释您要实现的目标。 Despite not understanding, I'll do my best to answer.虽然看不懂,还是尽量回答。

To do a browser redirect, just do window.location.href = " http://www.w3schools.com ";要进行浏览器重定向,只需执行 window.location.href = " http://www.w3schools.com ";

If you want to fetch a page and show the content of an external page, I think iframe would be the most straight-forward approach.如果您想获取页面并显示外部页面的内容,我认为 iframe 将是最直接的方法。

The one below is NOT RECOMMENDED if the response is a full html page as it's invalid HTML (ie can't have html, head, body inside body).如果响应是完整的 html 页面,则不推荐下面的页面,因为它是无效的 HTML(即,正文中不能包含 html、head、body)。 It'll also probably download unnecessary files.它也可能会下载不必要的文件。

Otherwise, create a property in the angular component, let's name it 'myHTML'.否则,在 angular 组件中创建一个属性,我们将其命名为“myHTML”。 Then bind that to the innerHTML property of a html element you create然后将其绑定到您创建的 html 元素的 innerHTML 属性

<div [innerHTML]="myHTML"></div>

Then once you fetch your URI (I recommend doing it directly -- GET to the final url, the way you did it is odd) just assign the response to the 'myHTML' property然后一旦你获取你的 URI(我建议直接做 - GET 到最终 url,你这样做的方式很奇怪)只需将响应分配给 'myHTML' 属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM