2016-10-22 76 views
0

即使我將視圖響應(AJAX調用)設置爲同一頁面上的div類,頁面也會重定向到新頁面(視圖)。 Ajax調用成功,我可以在控制檯中看到響應。將ajax響應設置爲div後重定向頁面

回到Home.jsp:

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Gen</title> 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script> 
<spring:url value="/resources/css/gen.css" var="mainCss" /> 
<spring:url value="/resources/js/gen.js" var="mainJs" /> 

<link href="${mainCss}" rel="stylesheet" /> 
<script type="text/javascript" src="${mainJs}"></script> 

</head> 
<body id="layout"> 
    <div id="header"> 
    <h1>XYZ</h1> 
</div> 
<div id="top-nav"> 
    <ul id="top-nav-list"> 
    <li class="top-nav-list-item" id="gen"> 
     <a class="top-nav-links" id="requestui" href="getrequestui">Generate</a> 
    </li> 
</ul> 
</div> 
<div id="container"> 
</div> 
</body> 
</html> 

gen.js

$(document).ready(function(){  

    $('#requestui').on('click',function(){ 
     var urlBuild = '/gen/getrequestui'; 
     $.ajax({ 
      url: urlBuild, 
      type: "POST", 
      async: false, 
      success: function (response) { 
       $("#container").html(response); 
      }, 
     }); 

    });  

}); 
+0

請參閱:您需要防止默認操作。 http://stackoverflow.com/questions/12648916/jquery-capture-anchor-href-onclick-and-submit-asynchronously和這裏http://stackoverflow.com/questions/788463/how-can-i-intercept-a使用ajax調用時鏈接與jQuery的聯繫 –

回答

0

更換

<a class="top-nav-links" id="requestui" href="getrequestui">Generate</a> 

<a class="top-nav-links" id="requestui" href="#">Generate</a> 
+0

您在href屬性中提供了url,因此在單擊超鏈接時將其重定向到新頁面。 –

+0

不正確。無論href如何,提問者都需要阻止默認的點擊操作。 –

+0

謝謝,其工作 –