溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用原生javascript怎么實(shí)現(xiàn)一個ajax請求

發(fā)布時間:2021-04-06 16:20:41 來源:億速云 閱讀:144 作者:Leah 欄目:web開發(fā)

本篇文章給大家分享的是有關(guān)使用原生javascript怎么實(shí)現(xiàn)一個ajax請求,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

具體如下:

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <table id="t">
    <tr>
      <td>學(xué)號:</td>
      <td><input type="text" id="stuid" /></td>
    </tr>
    <tr>
      <td>密碼:</td>
      <td><input type="password" id="stupass" /></td>
    </tr>
    <tr>
      <td colspan="2">
        <input id="btnLogin" type="button" value="登錄" />
      </td>
    </tr>
  </table>
</body>
</html>

ajax的一般步驟

1、創(chuàng)建對象

let xhr = new XMLHttpRequest();

2、設(shè)置請求參數(shù)

xhr.open(請求方式,請求地址,是否異步);

3、設(shè)置回調(diào)函數(shù)

xhr.onreadystatechange = function () {
    // 5、接收響應(yīng)
  alert(xhr.responseText);
}

4、發(fā)送

xhr.send();
<script type="text/javascript">
window.onload = function(){
  $("#btnLogin").onclick = function(){
    //1、創(chuàng)建對象
    let xhr = new XMLHttpRequest();
    //2、設(shè)置請求參數(shù)
    xhr.open('post','loginCheckajax.php',true);
    //3、設(shè)置回調(diào)函數(shù)
    xhr.onreadystatechange = function(){
      if(xhr.readyState==4 && xhr.status==200){
        if(xhr.responseText=='1'){
          //存cookie
          saveCookie("username",$("#stuid").value,7);
          //挑到首頁
          location.href="index.html" rel="external nofollow" ;
        }else{
          alert("登錄失敗!");
        }
      }
    }
    xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
    //4、發(fā)送
    xhr.send("stuid="+$("#stuid").value+"&stupass="+$("#stupass").value);
  }
}
function $(str){  //id class tagname
  if(str.charAt(0) == "#"){
    return document.getElementById(str.substring(1));
  }else if(str.charAt(0) == "."){
    return document.getElementsByClassName(str.substring(1));
  }else{
    return document.getElementsByTagName(str);
  }
}
</script>

php文件

<?php
  header("Content-type:text/html;charset=utf-8");
  //一、獲取用戶的輸入
  $stuid = $_POST['stuid'];
  $stupass = $_POST['stupass'];
  //二、處理
  //1、建立連接(搭橋)
  $conn = mysql_connect('localhost','root','root');
  if(!$conn){
    die("連接失敗");
  }
  //2、選擇數(shù)據(jù)庫(選擇目的地)
  mysql_select_db("mydb1809",$conn);
  //3、執(zhí)行SQL語句(傳輸數(shù)據(jù))
  $sqlstr="select * from student where stuid='$stuid' and stupass='$stupass'";
  $result = mysql_query($sqlstr,$conn);//結(jié)果是個表格
  //4、關(guān)閉數(shù)據(jù)庫(過河拆橋)
  mysql_close($conn);
  //三、響應(yīng)
  if(mysql_num_rows($result)>0){
    echo "1";
  }else{
    echo "0";
  }
?>
<!-- 保存cookie -->
<script>
  // saveCookie
//保存cookie
//參數(shù):
//鍵
//值
//有效期(單位是:天)
//返回值:無
function saveCookie(key,value,dayCount){
  var d = new Date();
  d.setDate(d.getDate()+dayCount);
  document.cookie = key+'='+escape(value)+';expires='+d.toGMTString();
}
//獲取cookie(根據(jù)鍵獲取值)
//參數(shù):
//鍵
//返回值:值
function getCookie(key){
  var str = unescape(document.cookie);//username=jzm; userpass=1236667
  //1、分割成數(shù)組(; )
  var arr = str.split('; ');//['username=jzm','userpass=1236667']
  //2、從數(shù)組查找key=;
  for(var i in arr){
    if(arr[i].indexOf(key+'=')==0){
      return arr[i].split('=')[1];
    }
  }
  return null;
}
//刪除cookie
//參數(shù):
//鍵
//返回值:無
function removeCookie(key){
  saveCookie(key,'',-1);
}
</script>

以上就是使用原生javascript怎么實(shí)現(xiàn)一個ajax請求,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI