2009年10月28日 星期三

[javascript] Ajax

// ajax 函數
function GetXmlHttpObject(){

var xmlHttp=null;

try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}

=====================================================
使用函數
function module_generate(str){
xmlHttp3=GetXmlHttpObject()
if (xmlHttp3==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url = "ajax/ajax_issue_advanced_modules.php";
var val = document.getElementById("aproj").value;
var params = "proj="+val+"&t="+str+"&sid="+Math.random();
xmlHttp3.open("POST", url, true);
//Send the proper header information along with the request
xmlHttp3.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp3.setRequestHeader("Content-length", params.length);
xmlHttp3.setRequestHeader("Connection", "close");
xmlHttp3.onreadystatechange = function() {//Call a function when the state changes.
if(xmlHttp3.readyState == 4 && xmlHttp3.status == 200) {
var resp = xmlHttp3.responseText;
//alert(resp);
if (resp==2) {
document.getElementById("amod2").style.display="none";
document.getElementById("amodules").selectedIndex=0;
document.getElementById("aversion").selectedIndex=0;
document.getElementById("amoduleName").value='';
document.getElementById("amod1").style.display="block";
} else {
document.getElementById("amoduleName").value='';
document.getElementById("amod1").style.display="none";
document.getElementById("amod2").style.display="block";
document.getElementById("amod2").innerHTML = resp;
}
}
}
xmlHttp3.send(params);
}

【下列文章您可能也有興趣】

沒有留言: