2012-01-02 37 views
0

我想在使用Javascript的JSP視圖中使用AJAX。以下是我的JSP代碼signup.jsp:在Spring MVC視圖中使用AJAX將http.status視爲404

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%> 
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org /TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 

<script type="text/javascript"> 
var http; 
function someAjax(){ 
    alert("ITS still WORKING"); 

    if(window.XMLHttpRequest){ 
     http = new XMLHttpRequest(); 
    } 
    **http.open("GET", "AjaxSameUser.jsp", true);**  
    alert("After connection"); 
    http.onreadystatechange=handleResponse; 
    http.send(null); 
} 

function handleResponse(){ 
    alert("readyState :" + http.readyState+" status : "+http.status); 
    if(http.readyState==4 && http.status==200){ 
     alert("State as expected"); 
     document.getElementById("usernameExists").innerHTML = http.responseText; 
    } 
} 
</script> 
</head> 
<body> 
<form:form commandName="user" method="POST"> 
    <table> 
     <tr>  
      <td> Username : </td> 
      <td> <form:input path="name" id="username" onchange="someAjax()" /> </td> 
       <td> <div id="usernameExists"> Something already </div></td> 
     </tr> 
      </table> 
</form:form> 
</body> 
</html> 

SameUser.jsp有一些硬編碼的數據,它是在同一文件夾中signup.jsp。

我檢查了http.readyState和http.status。 readyState變爲4,但狀態依然爲404。

我知道,View的每個請求必須通過Spring MVC中的Controller。但是爲什麼我不能直接從Javascript調用一個JSP頁面而不需要Spring的干預?

或者,我該如何在Spring MVC中使用AJAX?我嘗試了this鏈接,但它說使用瓷磚,而我不熟悉它,因爲我是新的Spring。

任何人都可以提出一些好方法。

+0

哪裏是你AjaxSamUser.jsp?它在WEB-INF內部嗎? – kosa 2012-01-02 13:31:55

+0

@thinksteep:不。它與signup.jsp位於同一個文件夾中。兩者都在/ WEB-INF/AllJsp /文件夾中。 – whitehat 2012-01-03 08:46:57

回答

0

我認爲這是URL的問題。嘗試直接從瀏覽器訪問您的網址(使用螢火蟲像獲得ajax網址的插件)。如果問題是與URL然後糾正

http.open("GET", "AjaxSameUser.jsp", true);