Ajax简单示例
JSP:
<!-- 搜索框 -->
<div class="col-xs-6 pull-right">
<div class="input-group">
<label for="beginTime" style="margin-right:15px">DeviceIP</label> <input type="text"
class="form-control" id="deviceIp" name="deviceIp" placeholder="please input DeviceIP">
<label for="owner" style="margin-right:15px"> DeviceAlias</label>
<input type="text" class="form-control" id="deciceName" placeholder="please input DeviceAlias">
<span class="input-group-btn">
<button id="go" class="btn btn-default search" type="button">Go!</button>
</span>
</div>
</div>
JS:
$(function(){
$("#go").click(function(){
var deviceIp = $("#deviceIp").val();
var deviceAlias = $("#deciceName").val();
$.ajax({
url:"${pageContext.request.contextPath}/device/searchList",
type:'post',
dataType: "json",
data:{
"deviceIp":deviceIp,
"deviceAlias":deviceAlias
},
success: function(data){
alert(success);
$.each(data,function(index,element){
alert(element.deviceIp);
alert(element.deviceAlias);
})
},
error:function(){
alert(error);
}
});
});
});
Controller:
@RequestMapping(value = "/searchList",method = RequestMethod.POST)
private String searchList(Model model,HttpServletRequest request,
HttpServletResponse response) throws Exception
{
response.setCharacterEncoding("UTF-8");
String searchIp = request.getParameter("deviceIp");
String searchAlias = request.getParameter("deviceAlias");
System.out.println(searchIp+" : "+searchAlias);
return "searchList";
}