1.ajax原生函数
function InitAjax(){ var ajax=false; try { ajax = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { ajax = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { ajax = false; } } if (!ajax && typeof XMLHttpRequest!='undefined') { ajax = new XMLHttpRequest(); } return ajax;} var ajax = InitAjax();ajax.open("GET", url, true);ajax.onreadystatechange = function() { if (ajax.readyState == 4 && ajax.status == 200) { var show = document.getElementById("divid"); show.innerHTML = ajax.responseText; }}ajax.send(null);方法二
function_post(Url, Args){var xmlhttp;var error;if(window.XMLHttpRequest){xmlhttp =newXMLHttpRequest();}elseif(typeofActiveXObject !="undefined"){eval('try {xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {xmlhttp = null;error=e;}');}if(typeof(Args)=='undefined'){ Args ='null=true';}if(null!= xmlhttp){xmlhttp.open("POST", Url,false);xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");xmlhttp.send(Args);strText = xmlhttp.responseText;}returnstrText;}
使用方法很简单,方法如下:
<script language="javascript">
var data = _post("ajax.php"); alert(data); </script>这样,_post函数返回了ajax.php的输出结果。
ajax.php页面的内容大致可以这样写:
<?php
header("Content-type: text/xml; charset=gb2312"); echo 'This a string.'; ?>