AJAX向Servlet傳輸中文參數解決方案
function checkMember(){
var username = document.getElementById("userid").value;
var password = document.getElementById("password").value;
var password2 = document.getElementById("repeat").value;
var email = document.getElementById("email").value;
var phone = document.getElementById("phone").value;
var addr = document.getElementById("addr").value;
getResult(username,password,phone,addr,email);
}
function createXmlHttp() {
//根據window.XMLHttpRequest對象是否存在使用不同的創建方式
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest(); //FireFox、Opera等瀏覽器支持的創建方式
} else {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");//IE瀏覽器支持的創建方式
}
}
function getResult(username,password,phone,addr,email){
createXmlHttp();
var param = "flag=add&username="+username+"&password="+password+"&phone="+phone+"&addr="+addr+"&email="+email;
var url = "/ShoppingWeb/servlet/AdminServlet?random="+Math.random(); //創建XMLHttpRequest對象
xmlHttp.open("POST",url,true);
xmlHttp.onreadystatechange = callback;
//設置HTTP的Content-Type='application/x-www-form-urlencoded' 為發送的內容編碼
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
//使用XMLHttpRequest對象的send()方法,傳輸數據
xmlHttp.send(param);
}
function callback(){
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
if(xmlHttp.responseText == 1){
alert("注冊成功!");
}
}
}
}