首先獲取xmlHttpapp
//建立xmlHttp function createXmlHttp() { var xmlHttp = null; if (window.ActiveXObject) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } else if (window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } return xmlHttp; }
若是使用get方法的話,能夠寫做:post
function a(number){ var xmlHttp = createXmlHttp(); xmlHttp.onreadystatechange = function() { if (xmlHttp.readyState == 4) { if (xmlHttp.status == 200) { } } }; var url = "a.action?a.number="+number; xmlHttp.open("GET", url, true); xmlHttp.send(null); }
若是使用post方法的話,能夠寫做:url
function b(number){ var xmlHttp = createXmlHttp(); xmlHttp.onreadystatechange = function() { if (xmlHttp.readyState == 4) { if (xmlHttp.status == 200) { } } }; var url = "b.action"; var string = "b.number=" + number; xmlHttp.open("POST", url, true); //失去這一條POST就沒法識別 xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xmlHttp.send(string); }