DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> AJAX入門 >> AJAX基礎知識 >> 利用Ajax實現在腳本裡傳值實例介紹
利用Ajax實現在腳本裡傳值實例介紹
編輯:AJAX基礎知識     
頁面腳本
復制代碼 代碼如下:
function ajaxSave(URLS) {
//定義一個變量用於存放XMLHttpRequest對象
var xmlhttp;
//定義一個變量用於存放 從服務器返回的響應結果
var responseContext = "";
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

responseContext = xmlhttp.responseText;
alert(responseContext);
}
}

xmlhttp.open("POST", URLS, true);
xmlhttp.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
xmlhttp.send();
}

後台方法
復制代碼 代碼如下:
Boolean boolean1;
String reponseText = "";
if(boolean1){
reponseText="保存成功!";
}
else{
reponseText="保存失敗!";
}
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.println(reponseText);
out.flush();
out.close();
return null;
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved