Latest web development tutorials

ASP - AJAX 및 ASP

AJAX는 더 많은 대화 형 응용 프로그램을 만드는 데 사용됩니다.


AJAX ASP 예제

하기 실시 예는 설명 할 것이다 때 웹 페이지는 웹 서버와 통신하는 방법에 문자 입력 창에 사용자 유형 :

Start typing a name in the input field below:

First name:

Suggestions:



예로 설명 - HTML 페이지

상기 입력 창에 문자에서 사용자는 "인 showHint ()"기능을 수행하는 경우. 이 기능은 트리거 이벤트 "onKeyUp에"로 구성

<!DOCTYPE html>
<html>
<head>
<script>
function showHint(str)
{
if (str.length==0)
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","gethint.asp?q="+str,true);
xmlhttp.send();
}
</script>
</head
<body>

<p><b>Start typing a name in the input field below:</b></p>
<form>
First name: <input type="text" onkeyup="showHint(this.value)" size="20">
</form>
<p>Suggestions: <span id="txtHint"></span></p>

</body>
</html>

자료 설명 :

입력 상자가 (str.length == 0) 비어있는 경우,이 함수는 txtHint 콘텐츠 개체 틀을 취소 한 기능을 종료합니다.

입력 박스가 비어 있지 않은 경우 인 showHint ()는 다음과 같이 수행한다 :

  • XMLHttpRequest 객체를 생성
  • 서버가 응답을 수행 할 준비가되었을 때 기능 만들기
  • 요청을 전송하는 서버에 파일
  • 그 URL 매개 변수의 끝을 (입력 상자 포함) (Q)에 추가주의하시기 바랍니다

ASP 파일

이 페이지를 호출하는 자바 스크립트를 통해 상기 서버는 "gethint.asp"ASP 파일이라고합니다.

"Gethint.asp"소스 코드 이름의 배열을 확인하고 이름을 대응하는 브라우저로 되돌아 :

<%
response.expires=-1
dim a(30)
'Fill up array with names
a(1)="Anna"
a(2)="Brittany"
a(3)="Cinderella"
a(4)="Diana"
a(5)="Eva"
a(6)="Fiona"
a(7)="Gunda"
a(8)="Hege"
a(9)="Inga"
a(10)="Johanna"
a(11)="Kitty"
a(12)="Linda"
a(13)="Nina"
a(14)="Ophelia"
a(15)="Petunia"
a(16)="Amanda"
a(17)="Raquel"
a(18)="Cindy"
a(19)="Doris"
a(20)="Eve"
a(21)="Evita"
a(22)="Sunniva"
a(23)="Tove"
a(24)="Unni"
a(25)="Violet"
a(26)="Liza"
a(27)="Elizabeth"
a(28)="Ellen"
a(29)="Wenche"
a(30)="Vicky"

'get the q parameter from URL
q=ucase(request.querystring("q"))

'lookup all hints from array if length of 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

'Output "no suggestion" if no hint were found
'or output the correct values
if hint="" then
response.write("no suggestion")
else
response.write(hint)
end if
%>

설명 : 자바 스크립트 텍스트 (즉, 나 strlen ($의 q)를> 0) 보낼 경우 발생

  1. 자바 스크립트의 이름을 보낼 문자를 일치 찾기
  2. 일치하는 항목이없는 경우, 응답 문자열은 "아니오 제안"으로 설정
  3. 당신은 응답 문자열을 하나 이상 일치하는 이름, 모든 설정의 이름을 발견 한 경우
  4. 은 "txtHint"자리에 대한 응답을 보내기