Hi,
After a form is submitted, I'm using AJAX to forward the form data to my
Struts Action class. The Struts processes the data and returns a responseText back to the original
jsp page.
The problem I'm having is that, when the responseText returns the page gets forwarded to a blank page displaying the value of the reponseText object. I don't know why the javascript ONREADYSTATE function is not getting the responseText. The user should remain on the same original form page; they should not be forwarded to this new blank page.
Pls...Help
Here's my code:
....
var req = false;
//pre-load image on the server
var progress_bar = new Image();
progress_bar.src = 'http://localhost:8080/PBWebApp/web/images/color-circle.gif';
function replace_html(id, content) {
document.getElementById(id).innerHTML = content;
}
function show_progressbar(id) {
document.forms['FileUploadForm'].uploadButton.disabled = true;
//document.forms['FileUploadForm'].submit.disabled = true;
replace_html(id, '<img src="http://localhost:8080/PBWebApp/web/images/color-circle.gif" border="0" /><i>Loading, please wait...</i>');
}
function success_handler() {
document.forms['FileUploadForm'].uploadButton.disabled = false;
//document.forms['FileUploadForm'].submit.disabled = false;
replace_html('content', '<font color="red"><i>Video upload successful.</i></font>');
}
function failure_handler() {
document.forms['FileUploadForm'].uploadButton.disabled = false;
//document.forms['FileUploadForm'].submit.disabled = false;
replace_html('content', '<font color="red"><i>Video upload failed. File may be corrupted.</i></font>');
}
function validateVideoFile()
{
var resp = new Boolean(false);
re=/.(mov|avi|wmv|mpe?g)$/
if( re.test(document.getElementById("fileName").value) == false )
{
alert("You may only upload .mov, .avi, .mpeg or .wmv images (case-sensitive)");
resp = false;
} else {
send_request();
}
//return resp;
return true;
}
/*
* Get the second options by calling a Struts action
*/
function send_request()
{
//display progress bar
show_progressbar('content');
url="http://localhost:8080/PBWebApp/uploadVideo.do";
var params = null;
//var params = document.forms['FileUploadForm'].fileName.name + "=" + document.forms['FileUploadForm'].fileName.value;
//params = "&" + params + document.forms['FileUploadForm'].businessId.name + "=" + document.forms['FileUploadForm'].businessId.value;
//Do the Ajax call
if (window.XMLHttpRequest){ // Firefox, Safari, ...
this.req = new XMLHttpRequest();
//A call-back function is define so the browser knows which function to call after the server gives a reponse back
this.req.onreadystatechange = function (){
if (this.req.readyState == 4 ) // Complete
{
if (this.req.status == 200) // OK response
{
var status = this.req.responseText;
alert("data1: " + status);
if( status == 'success' )
{
success_handler();
}
else if( status == 'failure' ) {
failure_handler();
}
}
}
};
try {
this.req.open("GET", url, false);
//this.req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
//this.req.setRequestHeader("Content-length", params.length);
//this.req.setRequestHeader("Connection", "close");
} catch (e) {
alert(e);
}
this.req.send(params);
.....
STRUTS ACTION:
public ActionForward execute(ActionMapping mapping_,
ActionForm form_, HttpServletRequest request_,
HttpServletResponse response_) throws Exception {
//...doing some business processing here
PrintWriter out = response_.getWriter();
String outLine = (stat == true) ? "success" : "failure";
System.out.println("Response String - " + outLine);
//out.print(outLine);
out.write(outLine);
forward = null;
return mapping_.findForward(forward);