Javascript Ajax在响应后返回主页

Javascript Ajax在响应后返回主页,javascript,jquery,html,ajax,spring,Javascript,Jquery,Html,Ajax,Spring,对不起,如果我在这件事上这么笨的话 我只是尝试功能,然后才有一个适当的结构 在输入页面中单击登录时,它应该调用一个ajax jsp。 我把它打印出来以备核实。 打印之后。它会返回到主页 这是我的欢迎页面。 http://localhost:8080/Example/ 发出警报后,它会返回到 http://localhost:8080/Example/? 我在春季MVC中尝试过 Spring_servlet.xml <beans xmlns="http://www.springframewo

对不起,如果我在这件事上这么笨的话

我只是尝试功能,然后才有一个适当的结构

在输入页面中单击登录时,它应该调用一个ajax jsp。 我把它打印出来以备核实。 打印之后。它会返回到主页

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

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

我在春季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>
JSP


平静微风登录屏幕
函数doAjaxPost(){
$.ajax({
键入:“获取”,
url:“loajax”,
成功:功能(res){
警报(res);
},  
错误:函数(e){
警报('错误:'+e);
}  
});   
}  
欢迎


登录



此区域用于描述稍后可以修改的内容 果断的

关于Ajax,是否要更改页面?? 通常,在ajax返回方法中返回json或xml(仅数据realetd而不是html之类的东西)。我可以建议您,对于干净的解决方案,使用注释@RestController创建另一个控制器,在这里定义loadAjax函数,并在那里返回字符串,这样就可以了

@RestController
public class AjaxHandler {  

    @RequestMapping("/loajax")  
    public String serveAjax(HttpServletRequest request,HttpServletResponse res)
    { 
        System.out.println("hjhjhjh");
        return "loajax";  
    }
} 
为了简单和概念理解,我给出了这种类型的示例。另一方面,你也可以这样做。
希望有帮助。

您还可以提供更多的代码吗?@wong2我已经添加了它。您传递给ajax请求的url是错误的……它应该以“”开头。对于ajax调用,controller/loajax的语法也有点不同
<!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>
@RestController
public class AjaxHandler {  

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