我有一个MVC弹簧控制器。我在页面加载上称此为ajax。
$.ajax({
type: "get",
url: 'custom/topic',
data: "email1=" + email1 + "&email2=" + email2 ,
async: false,
success: function(result) {
alert(result);
},
error: function (xhr, desc, error) {
window.alert('description' + desc);
window.alert('error' + error);
}
});
我的控制器是:
@RequestMapping(value="/topic", method=RequestMethod.GET)
public String topic(
@RequestParam("x1") String x1,
@RequestParam("x2") String x2) {
System.out.println("test");
String result1 = custom.topic(x1, x2);
return "test";
在警报中成功(结果),它打印了页面的整个代码。
您ajax url和 topic
方法签名无效,
喜欢:
如果您有@RequestMapping(value="/topic", method=RequestMethod.GET)
,并且如果您类级映射@RequestMapping(value="/customprop")
也不要忘记添加。
然后定义表单动作标签中的get url:
<c:url value="/customprop/topic" var="getUrl"/>
<form action="${getUrl}" id="tellfriendform" method="GET">
,在您的ajax调用中,取回url:
$('#tellfriendform').submit(function(e){
var postData = $(this).serializeArray();
var formURL = $(this).attr("action");
$.ajax(
{
type: "GET",
url: formURL,
data: postData,
dataType: "json",
success: function(data) {
alert(JSON.stringify(data));
},
error:function(e){
alert('error: '+e);
}
});
e.preventDefault(); // STOP default action
e.unbind(); //unbind. to stop multiple form submit.
});
您应该更改主题方法签名,例如:
当您使用Spring 3.0.4时:然后,您应该看起来像:
@RequestMapping(value="/topic", method=RequestMethod.GET)
public @ResponseBody String topic(HttpServletRequest request,
HttpServletResponse response){
System.out.println("test");
String email = request.getParameter("email");
String email2 = request.getParameter("email2");
String result1 = custom.topic(email, email2);
return email+ ", "+email2;
}