I'm still learning how to use Ajax. My current project involves populating a dropdown based on the selected value from another dropdown in HTML. The issue I'm facing is that, while I am able to get the desired output, it includes unwanted HTML formatting.
Below is the JavaScript code I am using -
function showHint(str)
{
// alert("Ajax - " + str);
var xmlhttp;
if (str.length==0)
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","AjaxCall.jsp?q="+str,true);
xmlhttp.send();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert("xmlhttp.responseText - " + xmlhttp.responseText );
}
}
}
Currently, in the xmlhttp.responseText alert, I receive the following output -
I only require the output as follows -
D1&&&D2&&&D3&&&D4
I wish to eliminate the extra HTML code being returned.
This is what my AjaxCall.jsp looks like -
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="com.Search.Struts2.AccessCheckComponent"%>
<%@ page import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
response.setDateHeader("Expires", 0); // Proxies.
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
response.setContentType("text/plain");
%>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
System.out.println("-------------------jsp -------------- " );
String cpmt = request.getParameter("q");
System.out.println("cpmt -!!Ajax - jsp !! " + cpmt);
String deckStr = AccessCheckComponent.CheckCompartmentDeckRelationship(cpmt);
System.out.println("deckList -!!Ajax - jsp !! " + deckStr);
response.getWriter().write(deckStr.toString());
%>
</body>
</html>