1. 程式人生 > >原生JS Ajax 請求

原生JS Ajax 請求

else 對象 amp pan stat func ext val hang

       var username = document.getElementById(‘username‘).value;
            var password = document.getElementById(‘password‘).value;

            // 第一步:創建對象
            var xhr = null;
            if(window.XMLHttpRequest){
                xhr = new XMLHttpRequest();
            }else{
                xhr 
= new ActiveXObject("Microsoft.XMLHTTP"); } //初始化 var url = ‘./check.php?username=‘+username+"&password="+password; xhr.open(‘post‘,url,false); //這段代碼在xhr.send();執行完之後才能執行 xhr.onreadystatechange = function(){ if(xhr.readyState == 4){
if(xhr.status == 200){ alert(1); var data = xhr.responseText; if(data == 1){ document.getElementById(‘showInfo‘).innerHTML = ‘用戶名或者密碼錯誤‘; }else if(data == 2){ document.getElementById(
‘showInfo‘).innerHTML = ‘登錄成功‘; } } }; } xhr.send(null);

原生JS Ajax 請求