<input οnclick="sendAjax">
<script type="text/javascript">
alert(111);
function sendAjax(){
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("get","<%=basePath %>/manage/test/ajax",true);//true表示异步,false表示同步
xmlHttp.send();//GET xmlHttp.send("str");//POST
//callbackFunction
xmlHttp.onreadystatechange = function (){
var state = xmlHttp.readyState;//状态4种
var status = xmlHttp.status;//状态码
if(state == 4 && status == 200){
var data=xmlHttp.responseText;// 还有responseXML
document.getElementById("ID").value = data;
}
}
}
</script>
后台处理:
@RequestMapping("/ajax")
public void testAjax(HttpServletRequest request, HttpServletResponse response){
try {
response.setCharacterEncoding("utf-8");//响应字符集的编码格式
PrintWriter out=response.getWriter();
out.print("111");//发送响应数据
} catch (IOException e) {
e.printStackTrace();
}
}