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

Posted damoguying

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了原生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 = 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请求及服务端处理的主要内容,如果未能解决你的问题,请参考以下文章

Ajax--Ajax基于原生javascript:创建Ajax对象链接服务器发送请求接受响应结果

使用原生JS实现简单的ajax

JavaScript之AJAX:原生ajax入门

原生JS发送Ajax请求

原生JS发送Ajax请求JSONP

ajax技术详解,封装一个原生的ajax请求