jahlife
asked on
Onclick AJAX Call To ASP Script Then Redirect Page - Partially Working
Greetings,
Here is what we are trying to do:
1. Form with onclick event to call AJAX function - see code here
<input type="button" value="Remove image" onclick="update_data_event ('include/ bizamajig- update-dat a-event.as p')" id="Delete" name="Delete"/>
2. The AJAX/javascript function POSTs to URL, in this example, bizamajig-update-data-even t.asp.
2. The ASP script, bizamajig-update-data-even t.asp, updates the database then does a RESPONSE.WRITE(nextURL) as we want to communicate back to the AJAX call causing a redirection to the nextURL location.
Below is our supporting AJAX function called by the onclick event:
<!--
var http_request__update_data_ event = false;
var get_http_response__update_ data_event ;
function update_data_event(url) {
url = url + "?process_desc="+document. SubParentF orm.proces s_desc.val ue +"&lookup_type_desc="+docu ment.SubPa rentForm.l ookup_type _desc.valu e +"&view_by_identifier="+do cument.Sub ParentForm .view_by_i dentifier. value +"&Node="+document.SubPare ntForm.Nod e.value +"&sNode="+document.SubPar entForm.sN ode.value +"&Exp="+document.SubParen tForm.Exp. value +"&doc_group_id="+document .SubParent Form.doc_g roup_id.va lue +"&doc_id="+document.SubPa rentForm.d oc_id.valu e;
http_request__update_data_ event = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request__update_data_ event = new XMLHttpRequest();
if (http_request__update_data _event.ove rrideMimeT ype) {
http_request__update_data_ event.over rideMimeTy pe('text/x ml');
}
} else if (window.ActiveXObject) { // IE
try {
http_request__update_data_ event = new ActiveXObject("Msxml2.XMLH TTP");
} catch (e) {
try {
http_request__update_data_ event = new ActiveXObject("Microsoft.X MLHTTP");
} catch (e) {}
}
}
if (!http_request__update_dat a_event) {
alert("Cannot create an XMLHTTP instance");
return false;
}
http_request__update_data_ event.onre adystatech ange = alertContents;
http_request__update_data_ event.open ('POST', url, true);
http_request__update_data_ event.send ('');
}
function alertContents() {
if (http_request__update_data _event.rea dyState == 4) {
if (http_request__update_data _event.sta tus == 200) {
get_http_response__update_ data_event =http_requ est__updat e_data_eve nt.respons eText;
if (get_http_response__update _data_even t=="0"){
// document.write(http_reques t__update_ data_event .responseT ext);
document.all.myerror.style .display=" inline";
} else if (get_http_response__update _data_even t!="0"){
document.location.href=get _http_resp onse__upda te_data_ev ent;
}
} else {
alert("Please refresh your browser window. \n" + http_request__update_data_ event.stat usText);
}
}
}
//-->
Below is the RESPONSE.WRITE that the ASP script performs to send nextURL back to AJAX function for causing redirection/page refresh:
<%
nextURL="cm-dg-x-update.as p?process_ desc=" & CStr( Request.QueryString("proce ss_desc") ) & "&lookup_type_desc=" & CStr( Request.QueryString("looku p_type_des c") ) & "&view_by_identifier=" & CStr( Request.QueryString("view_ by_identif ier") ) & "&Node=" & CStr( Request.QueryString("Node" ) ) & "&sNode=" & CStr( Request.QueryString("sNode ") ) & "&Exp=" & CStr( Request.QueryString("Exp") ) & "&doc_group_id=" & CStr( Request.QueryString("doc_g roup_id") )
response.write(nextURL)
%>
Our problem is this - The AJAX function is successfully calling the ASP script and the database update is successful. However, when the ASP script performs the RESPONSE.WRITE with the nextURL, the AJAX function does not successfully parse and react, so no redirection/page refresh.
Please let me know if I can provide more background on the problem. Also, If you need to login to our website and see this page to debug, please let me know you need a password. Or I can insert some debug into the AJAX as needed.
Thanks for your help in advance.
Here is what we are trying to do:
1. Form with onclick event to call AJAX function - see code here
<input type="button" value="Remove image" onclick="update_data_event
2. The AJAX/javascript function POSTs to URL, in this example, bizamajig-update-data-even
2. The ASP script, bizamajig-update-data-even
Below is our supporting AJAX function called by the onclick event:
<!--
var http_request__update_data_
var get_http_response__update_
function update_data_event(url) {
url = url + "?process_desc="+document.
http_request__update_data_
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request__update_data_
if (http_request__update_data
http_request__update_data_
}
} else if (window.ActiveXObject) { // IE
try {
http_request__update_data_
} catch (e) {
try {
http_request__update_data_
} catch (e) {}
}
}
if (!http_request__update_dat
alert("Cannot create an XMLHTTP instance");
return false;
}
http_request__update_data_
http_request__update_data_
http_request__update_data_
}
function alertContents() {
if (http_request__update_data
if (http_request__update_data
get_http_response__update_
if (get_http_response__update
// document.write(http_reques
document.all.myerror.style
} else if (get_http_response__update
document.location.href=get
}
} else {
alert("Please refresh your browser window. \n" + http_request__update_data_
}
}
}
//-->
Below is the RESPONSE.WRITE that the ASP script performs to send nextURL back to AJAX function for causing redirection/page refresh:
<%
nextURL="cm-dg-x-update.as
response.write(nextURL)
%>
Our problem is this - The AJAX function is successfully calling the ASP script and the database update is successful. However, when the ASP script performs the RESPONSE.WRITE with the nextURL, the AJAX function does not successfully parse and react, so no redirection/page refresh.
Please let me know if I can provide more background on the problem. Also, If you need to login to our website and see this page to debug, please let me know you need a password. Or I can insert some debug into the AJAX as needed.
Thanks for your help in advance.
ASKER
laneway,
Thanks very much for translating this into JQuery with your added comments. The page already includes JQuery so I can test this immediately.
One quick question to help me understand your solution (as a newbie to JQuery):
How are you constructing the $.ajax call to receive results from the ASP script, when executing the alertContents callback function. Specifically, how is the ASP script return value generated by response.write(nextURL) or response.write("0") being received or mapped to the parameter, responseText, for the callback function, alertContents?
Thanks
Thanks very much for translating this into JQuery with your added comments. The page already includes JQuery so I can test this immediately.
One quick question to help me understand your solution (as a newbie to JQuery):
How are you constructing the $.ajax call to receive results from the ASP script, when executing the alertContents callback function. Specifically, how is the ASP script return value generated by response.write(nextURL) or response.write("0") being received or mapped to the parameter, responseText, for the callback function, alertContents?
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Clean solution and thanks for taking time to provide supportive explanations.
I would like to suggest that you use JQuery instead of writing your own custom AJAX code. JQuery is an free javascript library that does AJAX very well. It's a very good skill to learn. The code below shows you how to use it to do what you are try to do. You can download JQuery here: http://jquery.com/ - but i've attached it to this post for convenience.
Open in new window
jquery-1.4.2.min.js