Java Response.sendRedirct()URL未在浏览器上打开
用例:我在Java Response.sendRedirct()URL未在浏览器上打开,java,angular,spring-boot,Java,Angular,Spring Boot,用例:我在SpringBoot应用程序中有一个公共rest端点,它将用户重定向到Angular页面 我有以下代码来重定向来自我的RestController的请求: @RequestMapping(value = "/search", method = RequestMethod.POST) public ResponseEntity<?> search(HttpServletRequest request, HttpServletResponse response) {
SpringBoot
应用程序中有一个公共rest端点,它将用户重定向到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);
}
@RequestMapping(value=“/search”,method=RequestMethod.POST)
公共响应性搜索(HttpServletRequest请求,HttpServletResponse响应){
//一些逻辑
response.sendRedirect(“http://localhost:4200/#/entry");
返回新的响应状态(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>
<form method="POST" id="mySearchForm">
<input type="submit" value="Search">
</form>
<script>
document.getElementById('mySearchForm').action = "http://localhost:8080/api/search";
</script>
公共跟踪
搜寻
函数loadDoc(){
控制台日志(“点击按钮”);
var xhttp=newXMLHttpRequest();
xhttp.open(“POST”http://localhost:8080/api/search“,对);
xhttp.send();
}
从浏览器控制台和网络中,我可以看到接收到重定向请求,但页面未打开。我可以在响应主体中看到有角度的页面,但是,我如何在浏览器上获得它
PS:我看到其他帖子说
response.sendRedirect()
不起作用。在我的情况下,它正在工作,但我没有打开页面。它似乎正在处理表单提交。碰巧用以下代码进行了修复:
<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>
<form method="POST" id="mySearchForm">
<input type="submit" value="Search">
</form>
<script>
document.getElementById('mySearchForm').action = "http://localhost:8080/api/search";
</script>
document.getElementById('mySearchForm')。操作=”http://localhost:8080/api/search";