Doing an AJAX query
To let the browser Javascript file testWHS.js get data from the
server via an AJAX request, we need to add code in testWHS.js to make
the request and code in our server to answer it.
-
Let's start on the server side. The server will need to have the data if
it is going to answer questions about it, so let's deal with that first.
The entire list of 990 photos is in this file
photoList.json. Your server can read it in when it starts up.
Code for reading in the file using node.js is in
readFile.js. You need to integrate this into your server.
It keeps the list of photo names in a global variable called
"imgList". This is OK.
-
Next, let's get the server to answer queries of the form
http://server162.site:#####/query?num=372
where "372" can be any number between 0
and 989, and give a "bad query" response to anything else.
Take a look at your current dynamic query handling code.
If you have not already, break it out into its own function.
Then change it so that it looks for this specific kind of query, and
answers it using imgList. Check that it works by
typing some queries into the URL box of a browser.
-
Once we know the server can answer, we can change the
onclick function for the submit button in testWHS.js to make
an AJAX request to the server, rather than looking up the photo names
locally.
See the lecture notes from Monday, 5/7.