2011-04-21 67 views
3

您好所有我有以下代碼我無法運行AJAX例如

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
    <title> New Document </title> 
<script type="text/javascript"> 
function showHint(str) 
{ 
    var xmlhttp 
    if (window.XMLHttpRequest) 
    { 
     xmlhttp = new XMLHttpRequest() 
    } 
    else 
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 

     xmlhttp.onreadystatechange = function() 
     { 
     if (xmlhttp.readystate == 4 && xmlhttp.status == 200) 
     { 
      document.getElementById('hint').innerHTML= xmlhttp.responseText; 
     } 
     } 
     xmlhttp.open("GET","sample.aspx?q=" + str ,true) 
     xmlhttp.send() 
} 
</script> 
</head> 
<body> 
Type here: <input type="text" id="txt" onKeyUp = "showHint(this.value)"/> 
Suggestion here: <div id="hint"></div> 
</body> 
</html> 

但這個例子不是working..it是說訪問被拒絕(腳本錯誤) 如何解決這個..! 我的aspx頁面如下所示

<% 
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 
%> 

回答

0

您有兩個單獨的問題。首先,你的sample.aspx應該是sample.asp,因爲它是一個傳統的asp頁面,而不是.net asp頁面。請確保您更改了xmlhttp.open方法中的路徑。

其次,xmlhttp.readystate應該是xmlhttp.readyState - 注意大寫字母S.我花了點時間來弄清楚這一部分。

0


問題可能是您正在file://協議中運行HTML文件。據我所知,像.php & .asp這樣的服務器文件在file://協議中不起作用。
如果你真的想要這個工作,嘗試設置Apache並把你的文件放入你的服務器文件夾。如果你在Linux中,它在/ var/www中,我不太確定其他操作系統。
此外,認爲它是無關的,我會建議將您的Doctype改爲<!DOCTYPE html>,因爲它現在是標準。抱歉。你不需要。
祝你好運!