AJAX ASP 实例

AJAX ຖືກນຳໃຊ້ສຳລັບສ້າງຄວາມສຳພັນຫຼາຍຂື້ນ.

AJAX ASP 实例

ຕົວຢ່າງທີ່ຖືກສະແດງ:

ຄວາມປະກອບ

ກະລຸນາຂຽນຄຳຂັບ A-Z ໃນບານກ່າວຫຼັງນີ້:

ຊື່:

ຄຳແນະນຳຄົ້ນຫາ:

ການອະທິບາຍຄວາມຄິດ

ໃນຄູ່ຄວາມທີ່ວ່າ, ເວລາທີ່ຜູ້ນຳໃຊ້ໄດ້ຂຽນຄຳຂັບໃນບາງບານ, ຈະເກີດການດຳເນີນງານຟັງສັນຍາ "showHint()".

ຟັງສັນຍາສະຫຼາກລະບາຍດັ່ງກ່າວ.

ລາຍການທີ່ຖືກຕິດຕັ້ງຢູ່ນີ້:

ຄວາມປະກອບ

<html>
<head>
<script>
function showHint(str) {
    if (str.length == 0) { 
        document.getElementById("txtHint").innerHTML = "";
        return;
    }
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById("txtHint").innerHTML = this.responseText;
            }
        };
        xmlhttp.open("GET", "gethint.asp?q=" + str, true);
        xmlhttp.send();
    }
}
</script>
</head>
<body>
<p><b>ກະລຸນາເວົ້າພາສາ A-Z:</b></p>
<form> 
ຊື່:<input type="text" onkeyup="showHint(this.value)">
</form>
<p>ຄວາມສົນທະນາຄົ້ນຄວາມ:<span id="txtHint"></span></p>
</body>
</html>

ທ້າທາຍຕະຫຼອດຕົວເອງ

ການອະທິບາຍວິທະຍາ

ກ່ອນມັນຈະກວດກາບົດສະຫຼະຂໍ້ບໍ່ມີຄວາມຈິງຫຼັງຈາກນັ້ນ:str.length == 0);ຖ້າແມ່ນຈະກວດກາຄວາມຈິງຂອງ txtHint ແລະອອກຈາກຫົວຫນັງ.

ແຕ່ຖ້າບົດສະຫຼະຂໍ້ບໍ່ມີຄວາມຈິງຈະດຳເນີນການດັ່ງກ່າວຫຼັງຈາກນັ້ນ:

  • ສ້າງບັນດາບັນດາ XMLHTTPRequest
  • ສ້າງຫົວຫນັງທີ່ຈະດຳເນີນການເມື່ອບັນດາບັນດາບານສົ່ງຄຳຕອບຫຼັງຈາກທີ່ບັນດາບັນດາບານສົ່ງຄຳຕອບ
  • ສົ່ງການຮ້ອງຂໍຫາບັນດາ ASP ໃນທີ່ບານ(gethint.asp)
  • ບໍ່ມີຄວາມຈິງຈະຕ້ອງເພີ່ມມາບົດສະຫຼະ gethint.asp
  • str ສະຫຼຸບຂໍ້ຄວາມຂອງບົດສະຫຼະຂໍ້

ASP ດັ່ງກ່າວ - "gethint.asp"

ບັນດາ ASP ດັ່ງກ່າວນີ້ກວດກາບັນທຶກຊື່ຫຼັງຈາກນັ້ນຈະສົ່ງຊື່ຫຼັງຈາກນັ້ນຫາບານ

<%
 response.expires=-1
 dim a(32)
 ນຳມາບັນທຶກຊື່
 a(1)="Ava"
 a(2)="Brielle"
 a(3)="Caroline"
 a(4)="Diana"
 a(5)="Elise"
 a(6)="Fiona"
 a(7)="Grace"
 a(8)="Hannah"
 a(9)="Ileana"
 a(10)="Jane"
 a(11)="Kathryn"
 a(12)="Laura"
 a(13)="Millie"
 a(14)="Nancy"
 a(15)="Opal"
 a(16)="Petty"
 a(17)="Queenie"
 a(18)="Rose"
 a(19)="Shirley"
 a(20)="Tiffany"
 a(21)="Ursula"
 a(22)="Victoria"
 a(23)="Wendy"
 a(24)="Xenia"
 a(25)="Yvette"
 a(26)="Zoe"
 a(27)="Angell"
 a(28)="Adele"
 a(29)="Beatty"
 a(30)="Carlton"
 a(31)="Elisabeth"
 a(32)="Violet"
 ການຄົ້ນຫາ q ຈາກ URL
 q=ucase(request.querystring("q"))
 ການກວດສອບວ່າ hint, q ມີຄວາມຍາວກວ່າ 0
 if len(q)>0 then
   hint=""
   for i=1 to 30
     if q=ucase(mid(a(i),1,len(q))) then
       if hint="" then
         hint=a(i)
       else
         hint=hint & " , " & a(i)
       end if
     end if
   next
 end if
 ເມື່ອ hint="", ການສະແດງ "no suggestion", ຫຼື ການສະແດງຄວາມຈິງ
 if hint="" then
   response.write("no suggestion")
 else
   response.write(hint)
 end if
%>