Method to get make a simple ajax call and get a response
function openTermsAndConditions()
{
var selectedIndex = document.getElementById("planOptions").selectedIndex;
var selectedText = document.getElementById("planOptions").options[selectedIndex].text;
if(selectedText)
{
retrievePlanTermsURL(selectedText);
}
}
function retrievePlanTermsURL(planId)
{
url = '/MyServer/ajax.do?method=getPlanTermsAndConditionsURL' +
'&planId=' + planId;
//Do the Ajax call
req = ajaxRequestSend(url, "GET", openURL);
}
//Callback function
function openURL()
{
var termsURL = "";
if (req.readyState == 4) // Complete
{
if (req.status == 200) // OK response
{
termsURL = req.responseText;
document.getElementById("termcondURL").value = termsURL;
if(termsURL)
{
window.open(termsURL, "windowTermsAndConditions", "width=500,height=500");
}
}
}
}
function ajaxRequestSend(url, method, callBackFunciton){
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
req.open(method, url, true);
req.onreadystatechange = callBackFunciton;
req.send(null);
return req;
}
In AjaxAction.java, we add a simple method to return a value
public class AjaxAction extends Action
{
//...
private void getPlanTermsAndConditionsURL() throws Exception
{
String returnValue = DatabaseHelper.getSomeData();
response.setContentType("text/plain");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write(returnValue);
response.getWriter().flush();
}
}
In JSP, add an html select element with id =planOptions
No comments:
Post a Comment