1. [代碼]原生JS實現ajax 發送post請求php
<script> var oStr = ''; var postData = {}; var oAjax = null; //post提交的數據 postData = {"name1":"value1","name2":"value2"}; //這裏須要將json數據轉成post可以進行提交的字符串 name1=value1&name2=value2格式 postData = (function(value){ for(var key in value){ oStr += key+"="+value[key]+"&"; }; return oStr; }(postData)); //這裏進行HTTP請求 try{ oAjax = new XMLHttpRequest(); }catch(e){ oAjax = new ActiveXObject("Microsoft.XMLHTTP"); }; //post方式打開文件 oAjax.open('post','1.php?='+Math.random(),true); //post相比get方式提交多了個這個 oAjax.setRequestHeader("Content-type","application/x-www-form-urlencoded"); //post發送數據 oAjax.send(postData); oAjax.onreadystatechange = function(){ //當狀態爲4的時候,執行如下操做 if(oAjax.readyState == 4){ try{ alert(oAjax.responseText); }catch(e){ alert('你訪問的頁面出錯了'); }; }; }; </script>