Unable to send data from controller to javascript
This is the jsp,
<html>
<head>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
var a=12;
$(document).ready(function(){
$("button").click(function(){
alert("inside click");
$.post('test.htm',{inputNumber1: $("#inputNumber1").val()},function(data){
alert(data);
});
alert("returned");
});
});
</script>
</head>
<body>
<input id="inputNumber1" type="text" size="5">
<button>print</button><br>
</body>
</html>
And this is the controller
package web;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
public class TestController {
@RequestMapping(value="/test.htm")
public @ResponseBody Integer add(@RequestParam(value="inputNumber1", required=true) Integer inputNumber1,
Model model) {
System.out.println(inputNumber1);
return inputNumber1;
}
}
The inputNumber1 is being sent to the controller and it is printed, but it is not being sent back to the javascript, i.e.
function(data){ alert(data) };
, this function is not called and so is the alert.
I have included these two jar files apart from the usual spring jars,
jackson-mapper-asl-1.5.2.jar
spring-json-1.3.1.jar
Why isn't that function being called? Is there something more that I have to include? Please help, I'm new to spring3 and jquery.开发者_StackOverflow中文版
try this
public class TestController {
@RequestMapping(value="/test.htm")
public @ResponseBody int add(HttpServletRequest request) {
int num = Integer.valueOf(request.getParameter("inputNumber1"));
System.out.println(num);
return num;
}
}
The following jars have to be added to the classpath 1.jackson-core-asl-1.0.0.jar 2.jackson-mapper-asl-1.0.0.jar
and the following should be added to the dispatcher servlet
to enable annotation driven controllers, validation etc...
精彩评论