Ajax在响应后返回主页

Ajax returning to home page after response

本文关键字:返回 回主页 响应 Ajax      更新时间:2023-09-26

对不起,如果我在这方面这么笨。

我只是在拥有适当的结构之前尝试功能。

在输入页面中单击登录时,它应该调用 ajax jsp。我打印了它以示警觉以验证。打印后。它返回到主页。

这是我的欢迎页面。 http://localhost:8080/Example/

警报后,它返回到 http://localhost:8080/Example/?

我在Spring MVC中尝试过

Spring_servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/mvc 
   http://www.springframework.org/schema/mvc/spring-mvc.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
   ">
      <context:component-scan base-package="com.ksv" />
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean>
    <mvc:resources mapping="/resources/**" location="/resources/"  
    cache-period="31556926"/>
    <mvc:annotation-driven />
</beans>

控制器

package com.ksv;
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse; 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller  
public class Hello {  
    @RequestMapping("/")  
    public ModelAndView helloWorld() {  
        String message = "HELLO SPRING MVC HOW R U";  
        System.out.println("454545");
        return new ModelAndView("index");  
    }  

    @RequestMapping("/loajax")  
    public ModelAndView helloajax(HttpServletRequest request,HttpServletResponse res)
    { 
        System.out.println("hjhjhjh");
        return new ModelAndView("loajax");  
    }
}  

.JSP

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Calm breeze login screen</title>
<link rel="shortcut icon"
    href="${pageContext.request.contextPath}/resources/logo.ico">
<link rel="stylesheet"
    href="${pageContext.request.contextPath}/resources/css/style.css">
<script>  
   function doAjaxPost() {    
    $.ajax({  
     type : "Get",   
     url : "loajax",   
     success : function(res) {  
      alert(res);   
     },  
     error : function(e) {  
      alert('Error: ' + e);   
     }  
    });   
   }  
  </script>
</head>
<body>
    <div class="wrapper">
        <div class="container">
            <h1>Welcome</h1>
            <br>
            <form name="vinform">
                <input type="text" placeholder="Username"><br>
                <button id="login-button" onClick="doAjaxPost()">Login</button>
                <br>
                <h2>
                    <a href="inda.html">Create Account</a>
                </h2>
                <a href="inda.html">Forgot?</a><br> <br> <br> <span
                    id="ksv"> </span>
                <div class="img" align="center"></div>
                <h3>This area is used to describe something which can be later
                    decided</h3>
            </form>
        </div>
        <ul class="bg-bubbles">
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>
    <script
        src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
    <script src="${pageContext.request.contextPath}/resources/js/index.js"></script>


</body>
</html>

在 Ajax 上你想改变页面吗?通常你在ajax返回方法上返回json或xml(只有数据实现而不是html之类的东西)。我可以建议你,对于干净的解决方案,制作另一个带有注释@RestController的控制器,在这里定义loadAjax函数,然后在那里返回字符串,它将起作用

@RestController
public class AjaxHandler {  
    @RequestMapping("/loajax")  
    public String serveAjax(HttpServletRequest request,HttpServletResponse res)
    { 
        System.out.println("hjhjhjh");
        return "loajax";  
    }
} 

为了简单和概念的理解,我给出了这种类型的例子。其他明智的你也可以做同样的控制器。希望对您有所帮助。