AJAX mtaani wa ASP

AJAX 用于创建更具交互性的应用程序。

AJAX mtaani wa ASP

下面的例子演示:当用户在输入字段中键入字符时,网页如何与 web 服务器进行通信:

Mfano

请在下面的输入字段中键入字母 A-Z:

Jina:

Mazungumzo ya tafuta:

Muhtasari wa mivuno

Kwenye mivuno hivi, kama mtumishi anasaja heri kwenye eneo la kuingia, inafanywa kazi ya funetu ya "showHint()".

Funetu hii inasababisha kwa matukio ya onkeyup.

Hapa ni maelezo ya HTML:

Mfano

<html>
<head>
<script>
function showHint(str) {
    if (str.length == 0) { 
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else {
        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>Ingiza herufi A-Z katika kifungu cha chini:</b></p>
<form> 
Jina:<input type="text" onkeyup="showHint(this.value)">
</form>
<p>Mapendekezo ya utafutaji:<span id="txtHint"></span></p>
</body>
</html>

Jaribu bila kumwambia

Muhtasari wa kipengele cha programu:

Kwanza, tazama kama kifungu cha ingia hauwezi kusikia (str.length == 0);kama ni, ingiza kwa upofu maudhui ya txtHint na kumaliza muundo.

lakini, kama kifungu cha ingia hauwezi kusikia, tena:

  • Kapisha, watengeneza mbinu wa XMLHttpRequest
  • Kapisha, watengeneza muundo wa kufanya kazi ambao hutendeka kufanyika wakati server anasikia kwa kusema
  • Tuma mkataba wa ingia kwenye ASP file kwenye server (gethint.asp)
  • Tafadhali ingiza kati ya q katika gethint.asp
  • str variable huanza kusimamia maudhui ya kifungu cha ingia

ASP file - "gethint.asp"

Hii ASP file inaangalia array ya jina, na kumshambulia kwenye mifungo wa kibuka kwa jina:

<%
 response.expires=-1
 dim a(32)
 ingiza jina kwa kufill array
 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"
 Get q parameter from URL
 q=ucase(request.querystring("q"))
 Check if all hints in the array, the length of q is greater than 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
 If hint is not found, output "no suggestion" or output the correct value
 if hint="" then
   response.write("no suggestion")
 else
   response.write(hint)
 end if
%>