首页 > 代码库 > 原生javascript发送ajax请求及服务端处理

原生javascript发送ajax请求及服务端处理

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript">
//$j.getScript("${teamcityPluginResourcesPath}js/jquery-1.11.2.min.js");
its = {
writeSelect : function(obj) {
obj.options[0].selected = "select";
obj.options[0].text = obj.options[0].text
+ String.fromCharCode(event.keyCode);
event.returnValue = http://www.mamicode.com/false;
return obj.options[0].text;
},
ttt : function() {
var ss = document.getElementById("platform").value;
var jg = "";

if (ss == "") {
var aas = writeSelect(document.getElementById("platform"));
jg = aas;
} else {
jg = document.getElementById("platform").value;
}

alert(jg);
},
getAllCases: function(){
var path=document.getElementById("path").value;
var xmlhttp;
if (window.XMLHttpRequest){
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}else{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}


if (xmlhttp==null){
alert(‘your browser don not support AJAX!‘);
return;
}
var url=‘/testJSAjax/getCases.do‘;
xmlhttp.open("POST",url,true);
xmlhttp.setRequestHeader("Content-type","application/json");   //can‘t be application/json,charset=utf-8
xmlhttp.send(path);
xmlhttp.onreadystatechange=function (){
if(xmlhttp.readyState==1||xmlhttp.readyState==2||xmlhttp.readyState==3){
// 本地提示:加载中/处理中

}
if (xmlhttp.readyState==4 && xmlhttp.status==200){
var d=xmlhttp.responseText; // 返回值
// 处理返回值
}
}
}

}

</script>
<input type="text" id="path"></input><input type="button" onclick="its.getAllCases()"></input>
</body>
</html>

 

服务端处理:

public class AjaxController extends HttpServlet{
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String path = (String)req.getAttribute("path");//  the path‘s value will be null
StringBuffer jb = new StringBuffer();
String line = null;
try {
BufferedReader reader = req.getReader();
while ((line = reader.readLine()) != null)
jb.append(line);
} catch (Exception e) {/* report an error */
}
CaseMng caseMng = new CaseMng();
boolean update =caseMng.updateRepository("C:/test_svn");
String cases = caseMng.getAllCases(jb.toString());
// JSONObject jsonObj = new JSONObject();
// jsonObj.put("path", path);
Map map = new HashMap();
System.out.println(cases);
map.put("cases", cases);
Gson gson = new Gson();
String jsonObj = gson.toJson(map);
System.out.println("cases:"+cases);
this.sendJson(resp, jsonObj);
}

public void sendJson(HttpServletResponse response, String jsonobj) {
try {
PrintWriter out = response.getWriter();
out.print(jsonobj);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}

}
}

 

;charset=UTF-8

原生javascript发送ajax请求及服务端处理