【问题标题】:Response.sendRedirct() URL is not opening on the browserResponse.sendRedirect() URL 未在浏览器上打开
【发布时间】:2020-01-10 14:11:40
【问题描述】:

用例:我在 SpringBoot 应用程序中有一个公共休息端点,它将用户重定向到 Angular 页面。

我有以下代码来重定向来自我的RestController 的请求:

@RequestMapping(value = "/search", method = RequestMethod.POST)
public ResponseEntity<?> search(HttpServletRequest request, HttpServletResponse response) {

    // some logice

    response.sendRedirect("http://localhost:4200/#/entry");
    return new ResponseEntity(HttpStatus.OK);   
}

为了测试端点,我有一个带有以下代码的 HTML 页面:

<h1>Public Tracking</h1>
<button onclick="loadDoc()">Search</button>
<script>
    function loadDoc() {
        console.log("button clicked");
        var xhttp = new XMLHttpRequest();
        xhttp.open("POST", "http://localhost:8080/api/search", true);
        xhttp.send();
    }
</script>

从浏览器控制台和网络,我可以看到重定向的请求正在接收,但页面没有打开。我可以在响应正文中看到 Angular 页面,但是如何在浏览器上获取它?

PS:我看到其他帖子说response.sendRedirect() 不起作用。就我而言,它可以正常工作,但是我没有打开页面。

【问题讨论】:

    标签: java angular spring-boot


    【解决方案1】:

    它似乎正在处理表单提交。碰巧用以下代码修复:

    <form method="POST" id="mySearchForm">
        <input type="submit" value="Search">
    </form>
    
    <script>
        document.getElementById('mySearchForm').action = "http://localhost:8080/api/search";
    </script>
    

    【讨论】:

      猜你喜欢
      • 2015-05-22
      • 1970-01-01
      • 1970-01-01
      • 2012-07-13
      • 2013-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-27
      相关资源
      最近更新 更多