Day10 JS & ajax

(續(xù)) 上一節(jié)正則表達(dá)式

  • 貪婪模式
<script >
  let a="123456abc"
  let reg=/\d{3,6}/
  //貪婪模式:默認(rèn)取樣式中最大的值
  console.log(a.replace(reg, "*"))
  //懶惰模式:取樣式中最小值
  let noReg=/\d{3,6}?/
  console.log(a.replace(noReg,"*"))
</script>

1. JS內(nèi)置對(duì)象 Date

<script >
  let oDate=new Date()
  let year=oDate.getFullYear()
  let month=oDate.getMonth()+1  //月份要+1 從0開(kāi)始的
  let day=oDate.getDay()
  let date=oDate.getDate()
  let hour=oDate.getHours()
  let min=oDate.getMinutes()
  let second=oDate.getSeconds()
  console.log(oDate)
  console.log(year)
  console.log(month)
  console.log(day)
  console.log(date)
  console.log(hour)
  console.log(min)
  console.log(second)
</script>
  • 動(dòng)態(tài)時(shí)鐘Demo
<body>
<div>
  <img src="images/0.png" alt="">
  <img src="images/0.png" alt="">
  <img src="images/0.png" alt="">
  <img src="images/0.png" alt="">
  <img src="images/0.png" alt="">
  <img src="images/0.png" alt="">
</div>
<script >
 function showTime() {
   let clocks=document.getElementsByTagName("img")
   let oDate=new Date()
   let hour=oDate.getHours()
   let minute=oDate.getMinutes()
   let sec=oDate.getSeconds()

   //1.將時(shí)間變?yōu)樽址? 拼接起來(lái)
   //2.將他們分割成數(shù)組
   //3.數(shù)組遍歷 每個(gè)元素的內(nèi)容給到img的路徑
   //只要小于10,name就到數(shù)字前面補(bǔ)0
   function add(time){
     if (time<10){
       return "0"+time;
     }else{
       return time+""
     }
   }
   let allTime=add(hour)+add(minute)+add(sec)
   console.log(allTime)
   for (let i=0;i<allTime.length;i++){
     clocks[i].src="images/"+allTime[i]+".png"
   }
 }
 showTime();
 setInterval(showTime,1000)
</script>
</body>

2. AJAX

  • 2.1 JS原生AJAX

<script >
  let test=document.getElementById("test")
  //如何創(chuàng)使用ajax
  //1.創(chuàng)建ajax核心對(duì)象
  //2.建立與服務(wù)器的連接 open(method,url,是否異步 true)
  //3.發(fā)送請(qǐng)求
  //4.服務(wù)器端響應(yīng)
  //get

  let xhr=new XMLHttpRequest()
  let url="https://www.easy-mock.com/mock/5b3ae092d294426e05198b4c/ajaxTest/getTest"
  xhr.open("get",url,true)
  xhr.send()
  xhr.onreadystatechange=function () {
    if (xhr.readyState==4 && xhr.status==200){
      console.log(xhr.responseText)
      let resData=JSON.parse(xhr.responseText)
      test.innerHTML=resData.data.content;
    }

  }

  //post
  //使用原生post方式的時(shí)候需要設(shè)置一個(gè)請(qǐng)求頭  再open方法和send方法之間
  let postUrl="https://www.easy-mock.com/mock/5b3ae092d294426e05198b4c/ajaxTest/postTest"
  xhr.open("post",postUrl,true)
  xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
  xhr.send()
  xhr.onreadystatechange=function () {
    if ( xhr.status==200) {
      if (xhr.readyState==4){
        console.log(xhr.responseText)
        let postData=JSON.parse(xhr.responseText)
        console.log(postData.data.content)
      }
    }else{
      document.body.innerHTML=xhr.status;
    }
  }

  //get和post的區(qū)別
  //1.get請(qǐng)求的參數(shù)字段是再url里面的
  //2.安全性:post方法更安全
  //3.請(qǐng)求的數(shù)據(jù)量:post請(qǐng)求的數(shù)據(jù)量更大
  //4.get速度更快
</script>
</body>
  • 2.2 Jquery AJAX

記得引用 Jquery

<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>

  $.ajax({
    method:"get",
    url:"https://www.easy-mock.com/mock/5b3ae092d294426e05198b4c/ajaxTest/getTest",
    dataType:"json",
    success:function (res) {
      console.log(res)
    },
    error:function (xhr) {
      document.body.innerHTML=xhr.status
    }

  })
  • Jquery get
<script >
  let url="https://www.easy-mock.com/mock/5b3ae092d294426e05198b4c/ajaxTest/getTest";
  $.get(url, function (data) {
    console.log(data)
  }).fail(function (data) {
    console.log(data.status)

  })
</script>
  • Jquery post
<script >
  let url="https://www.easy-mock.com/mock/5b3ae092d294426e05198b4c/ajaxTest/postTest"
  $.post(url, function (data) {
    console.log(data)
  })
</script>
  • 2.3 Axios

  • 引用
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  • get方法
<script >
  let url="https://www.easy-mock.com/mock/5b3ae092d294426e05198b4c/ajaxTest/getTest"
 
  axios.get(url).then(function (success) {
    console.log(success)

  })
  .catch(function (error) {
    console.log(error)

  })

</script>
  • post 方法
 let postUrl="https://www.easy-mock.com/mock/5b3ae092d294426e05198b4c/ajaxTest/postTest"
 
axios.post(postUrl).then(function (success) {
    console.log(success)

  })
    .catch(function (error) {
      console.log(error)

    })

3.Ajax跨域問(wèn)題

  • 跨域問(wèn)題的原理


    跨域
    • 當(dāng)協(xié)議,子域名,主域名,端口號(hào),任意一個(gè)不同時(shí),就算作不同的域。
    • 不同域之間請(qǐng)求資源就算做跨域。
    • Javascript出于安全性的考慮,不允許跨域調(diào)用其他頁(yè)面的對(duì)象。簡(jiǎn)單理解就是因?yàn)镴avascript同源策略的限制,a.com域名下的js無(wú)法操作b.com域名下的對(duì)象。
  • 解決跨域
    jsonp

<script>
let url="https://api.douban.com/v2/book/search?q=javascript&count=1";
  $.ajax({
    type:"get",
    url:url,
    dataType:"jsonp",
    success:function (data) {
      console.log(data)
    }
  })
</script>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容