尽管我已经搜索了这个网站并尝试了所有方法,但仍不断出现上述错误。非常感谢一些帮助。问题在于 AddUser 页面上的 Jquery/JSON 请求。我已经添加了两个 Jackson jar 文件(核心和映射。)它部署在 Tomcat 7 上。
这是配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- Application Message Bundle -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages" />
<property name="cacheSeconds" value="3000" />
</bean>
<!-- Scans the classpath of this application for @Components to deploy as beans -->
<context:component-scan base-package="ajaxjqjsonbowling" />
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />
<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
这是控制器:
@RequestMapping(value="/AddUser.htm", method=RequestMethod.POST)
public @ResponseBody JsonResponse addUser(@ModelAttribute (value="player") Player player, BindingResult result){
JsonResponse res = new JsonResponse();
ValidationUtils.rejectIfEmpty(result, "name", "Name can not be empty.");
if(!result.hasErrors()){
htmlPlayerNo++;
player.setHtmlPlayerNo(htmlPlayerNo);
players.add(player);
res.setStatus("SUCCESS");
res.setResult(players);
}else{
res.setStatus("FAIL");
res.setResult(result.getAllErrors());
}
return res;
}
这些是来自 chrome 调试器的标头:
Request URL:http://localhost:8080/ajaxjqjsbowling/AddUser.htm
Request Method:POST
Status Code:406 Not Acceptable
Request Headersview parsed
POST /ajaxjqjsbowling/AddUser.htm HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 15
Accept: */*
Origin: http://localhost:8080
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31
Content-Type: application/x-www-form-urlencoded
Referer: http://localhost:8080/ajaxjqjsbowling/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: JSESSIONID=E7FBB01B3FA25CFC091AA539EDFE3CC9
Form Dataview sourceview URL encoded
name:John Horan
Response Headersview parsed
HTTP/1.1 406 Not Acceptable
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Length: 1067
Date: Mon, 06 May 2013 00:24:53 GMT
这是我的 JQuery 代码:
function doAjaxPost() {
// get the form values
var name = $('#name').val();
$.ajax({
type: "POST",
url: contexPath + "/AddUser.htm",
data: "name=" + name,
success: function(response){
// we have the response
if(response.status == "SUCCESS"){
userInfo = "<ol>";
for(var i =0; i < response.result.length ; i++){
userInfo += "<br><li><b>Name</b> : " + response.result[i].name;
}
userInfo += "</ol>";
$('#info').html("User has been added to the list successfully. " + userInfo);
$('#name').val('');
$('#error').hide('slow');
$('#info').show('slow');
}else{
errorInfo = "";
for(i =0 ; i < response.result.length ; i++){
errorInfo += "<br>" + (i + 1) +". " + response.result[i].code;
}
$('#error').html("Please correct following errors: " + errorInfo);
$('#info').hide('slow');
$('#error').show('slow');
}
},
error: function(e){
alert('Error '+ e);
}
});
}
添加用户.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>
<style>
h1 {
font-style: italic;
color: green;
font-family: Arial, Helvetica, sans-serif;
}
* {
color: blue;
font-family: Arial, Helvetica, sans-serif;
}
</style>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Ajax Jquery Bowling Game</title>
<script src="<%=request.getContextPath() %>/js/jquery.js"></script>
<script type="text/javascript">
var contexPath = "<%=request.getContextPath() %>";
</script>
<script src="<%=request.getContextPath() %>/js/player.js"></script>
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/style/app.css">
</head>
<body>
<div id = "topbox" style="width: 900x; height: 75px; background-color : #B7F39F">
<span id = "title"> Ajax-JQuery Bowling</span>
</div>
<h1>Welcome to The Bowling App!</h1>
<table>
<tr><td colspan="2"><div id="error" class="error"></div></td></tr>
<tr><td>Enter your name : </td><td> <input type="text" id="name"><br/></td></tr>
<tr><td colspan="2"><input type="button" value="Add Users" onclick="doAjaxPost()"><br/></td></tr>
<tr><td colspan="2"><div id="info" class="success"></div></td></tr>
</table>
<form:form method="post" action="/ajaxjqjsbowling/showPlay.htm">
<tr>
<td colspan="2">
<input type="submit" name = "button" value="Play"/>
</td>
</tr>
</form:form>
</body>
</html>