原生javascript發送ajax請求及服務端處理

來源:互聯網
上載者:User

標籤:res   處理   false   jquer   hashmap   flush   cat   void   option   

<%@ 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請求及服務端處理

相關文章

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。