Spring 无法从AJAX+;JQuery+;Jsp
我遇到的问题正如标题中提到的,我也尝试过在stackoverflow上搜索,但我并没有找到任何解决方案 我有一个Jsp页面Spring 无法从AJAX+;JQuery+;Jsp,spring,spring-mvc,controller,Spring,Spring Mvc,Controller,我遇到的问题正如标题中提到的,我也尝试过在stackoverflow上搜索,但我并没有找到任何解决方案 我有一个Jsp页面 <!DOCTYPE html> <html> <head> <title>CreateAccount</title> <script src="https://ajax.googleapis.com/ajax/libs/ext-core/3.1.0/ext-core.js"></script>
<!DOCTYPE html>
<html>
<head>
<title>CreateAccount</title>
<script src="https://ajax.googleapis.com/ajax/libs/ext-core/3.1.0/ext-core.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
var delay = (function(){
var timer = 0;
return function(callback, ms){
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
})();
$(document).ready(function(){
$("input.uname").keyup(function() {
delay(function(){
//alert('Hi, func called');
checkUsername();
}, 1000 );
});
});
function checkUsername() {
alert('go');
var requrl = "http://localhost:8082/VendorWebApplication/reqq3";
var greeting = {
"id" : 1,
"content" :"prasad"
}
$.ajax({
type: 'POST',
url: requrl,
data:JSON.stringify(greeting),
dataType: "text",
//dataType: "json", //tried with this also
contentType: 'application/json',
success: function (data) {
debugger;
var json = $.parseJSON(data);
alert(json.content);
},
error: function (data) {
debugger;
alert("error"+data);
}
});
}
</script>
</head>
<body>
//body content
</body>
</html>
但在这里,我的ajax请求甚至没有命中它
my web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>VendorWebApplication</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>VendorWebApplication</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
让我告诉你,当我发送带有数据的GET请求时,它正在命中控制器并捕获数据(使用method=RequestMethod.GET和all)。但是当我请求method=POST,它没有命中servlet映射中的,你是否尝试更改/by>*
<servlet-mapping>
<servlet-name>VendorWebApplication</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
VendorWebApplication
/*
我得到了答案,它是硬编码的,但可以工作
function checkUsername() {
alert('go');
var requrl ="http://localhost:8082/VendorWebApplication/checkUsername";
var greeting = {
"content" : "bhanu",
"id" :15
}
$.ajax({
type: 'POST',
url: requrl,
data:search,
dataType:'text',
success: function (data) {
alert(data);
},
error: function (data) {
alert("error"+data);
}
});
}
这里的意思是我们在接收时不转换为JSON
@RequestMapping(value ="/checkUsername",method=RequestMethod.POST)
public @ResponseBody String checkUsername(HttpServletRequest request) {
String content = request.getParameter("content");
int content = request.getParameter("id");
return "heyya";
}
这解决了我的问题,但它是硬编码的,它不会将Json文件映射/转换为问候类。如果有人得到了完美的答案,请在这里发布。本地主机上的服务器Tomcat v8.0服务器无法启动。原因:java.lang.IllegalArgumentException:Invalid*在servlet映射中尝试更改票证/*是我在web上的配置方式。xmlcause-HTTP状态404,顺便说一句,我认为这与我的问题无关许多人都有这个问题,因为servlet映射。对不起,我帮不了你,祝你好运
<servlet-mapping>
<servlet-name>VendorWebApplication</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
function checkUsername() {
alert('go');
var requrl ="http://localhost:8082/VendorWebApplication/checkUsername";
var greeting = {
"content" : "bhanu",
"id" :15
}
$.ajax({
type: 'POST',
url: requrl,
data:search,
dataType:'text',
success: function (data) {
alert(data);
},
error: function (data) {
alert("error"+data);
}
});
}
@RequestMapping(value ="/checkUsername",method=RequestMethod.POST)
public @ResponseBody String checkUsername(HttpServletRequest request) {
String content = request.getParameter("content");
int content = request.getParameter("id");
return "heyya";
}