user1296932
user1296932

Reputation:

W3Schools AJAX Examle not working on local Machine

Im trying to learn AJAX and going by the tutorial found on this site: http://www.w3schools.com/Ajax/ajax_aspphp.asp

I copy the html code on their site verbatim, and put it in an html file on my local drive: I also copied the "gethint.asp" file into the same folder. All the code is 100% the same as they have in their example. But when I run the html file I can see the text and text field but the asp functionality does not work at all, and it seems to work perfectly fine on thier site? What am I doing wrong?

<!DOCTYPE html>
<html>
<head>
<script>
function showHint(str)
{
var xmlhttp;
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>

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

</body>
</html>

gethint.asp file:

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

Upvotes: 0

Views: 1612

Answers (3)

yashhy
yashhy

Reputation: 3096

You need a server for running .asp file. First I suggest you to install servers like WAMP or XAMPP and then call the AJAX, your response will be shown.

Upvotes: 0

Michael St Clair
Michael St Clair

Reputation: 6625

To make server side code work on a local machine you will need to install a program to process the code. The program I use is xampp, here is a link http://www.apachefriends.org/en/xampp.html.

Upvotes: 0

Atutouato
Atutouato

Reputation: 375


The problem might be that you're running the HTML file in file:// protocol. As far as I'm aware, server files like .php & .asp don't work in file:// protocol.
If you really want this to work, try setting up Apache and put your files inside your server folder. If you're in Linux, it's in /var/www, with other operating systems I'm not so sure.

Best of luck!

Upvotes: 2

Related Questions