欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

原生javascript的ajax請(qǐng)求及后臺(tái)PHP響應(yīng)操作示例

 更新時(shí)間:2020年02月24日 08:51:02   作者:巴啦啦小能量  
這篇文章主要介紹了原生javascript的ajax請(qǐng)求及后臺(tái)PHP響應(yīng)操作,結(jié)合示例形式分析了JavaScript前臺(tái)ajax請(qǐng)求的原理、調(diào)用、后臺(tái)PHP響應(yīng)請(qǐng)求及cookie保存相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了原生javascript的ajax請(qǐng)求及后臺(tái)PHP響應(yīng)操作。分享給大家供大家參考,具體如下:

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <table id="t">
    <tr>
      <td>學(xué)號(hào):</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)建對(duì)象

let xhr = new XMLHttpRequest();

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

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

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)建對(duì)象
    let xhr = new XMLHttpRequest();
    //2、設(shè)置請(qǐng)求參數(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);
          //挑到首頁(yè)
          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ù)庫(kù)(選擇目的地)
  mysql_select_db("mydb1809",$conn);
  //3、執(zhí)行SQL語(yǔ)句(傳輸數(shù)據(jù))
  $sqlstr="select * from student where stuid='$stuid' and stupass='$stupass'";
  $result = mysql_query($sqlstr,$conn);//結(jié)果是個(gè)表格
  //4、關(guān)閉數(shù)據(jù)庫(kù)(過河拆橋)
  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>

更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript中ajax操作技巧總結(jié)》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)

希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論