在編寫ajax時遇到問題,部署到服務器上和本地是一樣的webapps,但運行服務器上代碼出錯,是緩存原因。
在大神教導下,原因總結如下:
- 編寫ajax時儘量使用jquery ajax, 它幫程序員屏蔽了瀏覽器的差異性
- IE瀏覽器空爲”“,谷歌瀏覽器空爲null
function cellphone_show()
{
var cellphone = document.getElementById("cellphone").value;
//建立一個xmlHttp核心對象
createXMLHttp();
//設置一個請求
xmlHttp.open("POST","CellphoneServlet", false);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); //!!!!後臺獲取不到參數,這個要寫
//設置請求完成之後處理的回調函數
xmlHttp.onreadystatechange=cellphone_Callback;
//發送請求,不傳遞任何參數
xmlHttp.send("cellphone="+cellphone);
}
function cellphone_Callback()
{
if(xmlHttp.readyState==4) //定義回調函數
{
if(xmlHttp.status==200) //正常HTML操作
{
/*if (!!window.ActiveXObject || "ActiveXObject" in window) //此方法兼容ie11
alert("我是 IE");
else
alert("我不是 IE"); */
//接受返回的內容
var text=xmlHttp.responseText;
// alert(text);
//設置要使用的CSS樣式表
//document.getElementById("msg").className="樣式表名稱";
document.getElementById("hidden_cellphone").value=text;
if(text != "ok") {
document.getElementById("cellphone_msg").innerHTML=text;
} else {
if (!!window.ActiveXObject || "ActiveXObject" in window)
document.getElementById("cellphone_msg").innerHTML="";
else
document.getElementById("cellphone_msg").innerHTML=null;
}
}
}
}
-
後面的參數服務器會忽略,但有參數的話,應用會從服務器拿數據,不會使用緩存。當然,也可以使用ctrl+F5強制刷新。