原生ajax中post方式數據傳遞方式

1.post方法中,數據放在send()方法中,作爲參數傳遞

2.需要設置請求頭,用來指明編碼方式,發送的數據類型

3.post方式不會出現中文亂碼和瀏覽器緩存問題

var ajax=null;
try {
    ajax=new XMLHttpRequest()
}catch(e) {
    ajax=new ActiveXObject("Microsoft.XMLHTTP")
}
ajax.open("post","a.php",true)
ajax.setRequestHeader("content-type","application/x-www-form-urlencoded")
ajax.send("name=張三&age=20")
ajax.onreadystatechange=function(){
    if(ajax.readyState==4){
        if(ajax.status==200){
            console.log(ajax.responseText)
        }
    }else{
        console.log(ajax.status)
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章