As is well known, Forms are the way to "call" server side scripts in HTML, the page will be reloaded each time the forms are posted, this is often not very user friendly. Using the HTTP Request, we can call the server side script without refreshing the page. I`ve used this technology in my current web project. However, I encountered some trouble with it, which took almost my whole night.
This is an online exam system that enables students to take online test through browser. It provides a feature that is enable students to mark doubtable questions which can be reviewed again. To prevent reloading pages, I use the HTTP Request to post requests to a server side script which deals with the answers of students, also the doubtable questions. The following is the main idea of this system:
First, Save all the questions selected randomly to a session with the doubtable flag set to the default value false.
And then, Display all the questions and their selectable answers, each with a button used to mark doubtable questions. The default value of the button is empty, when students click it, it will display a question mark. At the same time, set the doubtable field of the question to true in the session.
Now, the trouble comes. The first time students mark a doubtable question and cancel mark of the same question works well. From the second time on, It can not work correctly. I tried my best to find the root cause of it almost a whole night :(
The following is my initial code snippet:
In order to find out the reason, I add the following line to the server side script (dealAnswer.jsp):
<%=new java.util.Date()%>
And use the responseText property of HTTP Request Object to show the response. I imagine that when making a new request, the response should show a different time. Most curious of all, I found that this can work only the first time. From the second time on, it returned the same response as the time making the same request the first time. For example,
The First time students mark a doubtable question, the time of the response is:
Wed Jan 26 15:35:29 CST 2005
And then cancel the question mark of the same question, the time of the response is:
Wed Jan 26 15:36:52 CST 2005
From the second time on, when students mark the same doubtable question, the time of the response is still:
Wed Jan 26 15:35:29 CST 2005
And cancel the same question is still:
Wed Jan 26 15:36:52 CST 2005
It seems that the response has been cached. So, I added the following lines:
response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
However, NO EFFECT!!!!
Finally, I add an additonal request parameter named currTime to the Query String and delete the XML HTTP Request object after making the post so that there always be a different URL each time. As following:
Wow!!! It works!!!! I`m very excited.
Although it can work now, I don`t understand why. Any comments will be appreciated.