In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to solve the problem that SpringBoot does not jump when there is Ajax". In daily operation, it is believed that many people have doubts about how to solve the problem when SpringBoot does not jump when there is Ajax. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for everyone to answer the doubt that "SpringBoot does not jump when there is Ajax". Next, please follow the editor to study!
Problem description
Recently, I am trying to use SpringBoot to do a background management system, because I have not learned VUE yet, so the front-end page adopts the thymeleaf+ajax mode. Two days ago, I encountered a problem when developing the login page. After logging in, I should jump to the home page in the controller according to the normal process, but my login page did not jump. There is no problem with the logic of the controller.
@ Controllerclass LoginController {@ Resource lateinit var adminService: AdminService @ RequestMapping ("/ login", method = [RequestMethod.POST]) fun login (@ RequestParam name:String,@RequestParam password:String): String? {val admin=adminService.login (name,password) return if (admin==null) {"} else {/ / generally can be rendered directly to main.html But after adding Ajax, the jump will fail "main"}.
Logic in Ajax
$.ajax ({method: 'POST', url:' http://localhost:8080/login', data: {name: $('[name= "username"]') .val (), password: $('[name= "password"]') .val ()}, success:function (r) {console.log (r)}, error:function (result) {alert (result)}})
Information returned by the console
Solution method
The solution is given here, and the reason will be explained at the end. The solution is to add a mainPage method to the controller, corresponding to main.html.
@ RequestMapping ("/ main") fun mainPage (): String {return "main"}
Then call the controller in the success callback of Ajax to complete the jump.
Success:function (r) {_ window.location.href= "http://localhost:8080/main" rel=" external nofollow "}, reason combing
At first, I thought there was a problem written by the controller, and then I modified the configuration of the controller. Finally, I found that no matter how to modify it, it had no effect, and I found that if the controller path of main.html was called directly in the browser, there was no problem. In this case, I thought that it would be the problem of Ajax. In order to verify whether there is a problem with Ajax, a breakpoint is made in the code with a successful Ajax callback to see what the data returned by the backend is.
See, the controller returns the entire main.html page to Ajax, that is to say, when using Ajax, the SpringBoot controller does not render the page, but returns the target page structure, no wonder it can jump.
At this point, the study on "how to solve the problem of SpringBoot without jumping when there is Ajax" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.