简体   繁体   中英

Using AJAX in Spring MVC View gives http.status as 404

I want to use AJAX in a JSP View using Javascript. Following is my JSP code signup.jsp :

    <%@ 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 has some hard-coded data and it is in the same folder as signup.jsp.

I have checked http.readyState and http.status. readyState becomes 4, however status remains 404.

I know, every request for View must pass through the Controller in Spring MVC. But here why can't I directly call a JSP page from Javascript with no intervention of Spring?

Or rather how do I use AJAX in Spring MVC? I tried this link, but it says to use tiles and I am not familiar with it since I am new Spring.

Can anyone suggest some good way.

I think it's URL problem. Try to access your url directly from the browser (use firebug like addons for getting ajax urls). If the problem is with url then correct

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM