Dynamic response by java and handle using ajax jquery for all content types
Dynamic response by java and handle using ajax
jquery for all content types
Jquery ajax call
var url="trackDynamicResponse.do";
$.ajax({
url: url,
type: 'GET',
data :{
responseType : _responseType
},
success: function(response, status,
xhr){
var ct = xhr.getResponseHeader("content-type") || "";
if (ct.indexOf('html') > -1) {
console.log("html
...");
}
if (ct.indexOf('json') > -1) {
console.log("json
...");
}
if (ct.indexOf('xml') > -1) {
console.log("xml ...");
}
console.log(response);
$('#responseDiv').html("response ::
"+response.toString());
},
error :function( xhr, status, errorThrown ){
alert(" An error occured: " + errorThrown + ", Status :
" +
status);
console.log(errorThrown);
/*
if(errorThrown.indexOf("XML")>-1){
$('#responseDiv').html(errorThrown);
} */
}
});
---------------------------------------------------------------------------
Servlet :
public String trackDynamicResponse() throws IOException{
String responseType=request.getParameter("responseType");
if(responseType !=null && responseType.equalsIgnoreCase("JSON")){
JSONObject json = new JSONObject();
JSONArray addresses = new JSONArray();
JSONObject address;
int count = 15;
for (int i=0 ; i<count ; i++)
{
address = new JSONObject();
address.put("CustomerName" , "Decepticons" + i);
address.put("AccountId" , "1999" + i);
address.put("SiteId" , "1888" + i);
address.put("Number" , "7" + i);
address.put("Building" , "StarScream Skyscraper" + i);
address.put("Street" , "Devestator Avenue" + i);
address.put("City" , "Megatron City" + i);
address.put("ZipCode" , "ZZ00 XX1" + i);
address.put("Country" , "CyberTron" + i);
addresses.add(address);
}
json.put("Addresses", addresses);
response.setContentType("application/json");
response.getWriter().write(json.toString());
}else if(responseType !=null && responseType.equalsIgnoreCase("XML")){
response.setContentType("application/xml");
String xml = "";
xml = xml + "<response>";
xml = xml + " This is XML Response";
xml = xml + "</response>";
response.getWriter().write(xml);
}else if(responseType !=null && responseType.equalsIgnoreCase("HTML")){
String html = "<html><body>";
html = html + "This is <b>HTML</b> Response";
html = html + "</response>";
response.setContentType("text/html");
response.getWriter().write(html);
}else{
response.getWriter().write("default text Response type");
}
return null;
}
---------------------------------------------------------------------------
Servlet :
public String trackDynamicResponse() throws IOException{
String responseType=request.getParameter("responseType");
if(responseType !=null && responseType.equalsIgnoreCase("JSON")){
JSONObject json = new JSONObject();
JSONArray addresses = new JSONArray();
JSONObject address;
int count = 15;
for (int i=0 ; i<count ; i++)
{
address = new JSONObject();
address.put("CustomerName" , "Decepticons" + i);
address.put("AccountId" , "1999" + i);
address.put("SiteId" , "1888" + i);
address.put("Number" , "7" + i);
address.put("Building" , "StarScream Skyscraper" + i);
address.put("Street" , "Devestator Avenue" + i);
address.put("City" , "Megatron City" + i);
address.put("ZipCode" , "ZZ00 XX1" + i);
address.put("Country" , "CyberTron" + i);
addresses.add(address);
}
json.put("Addresses", addresses);
response.setContentType("application/json");
response.getWriter().write(json.toString());
}else if(responseType !=null && responseType.equalsIgnoreCase("XML")){
response.setContentType("application/xml");
String xml = "";
xml = xml + "<response>";
xml = xml + " This is XML Response";
xml = xml + "</response>";
response.getWriter().write(xml);
}else if(responseType !=null && responseType.equalsIgnoreCase("HTML")){
String html = "<html><body>";
html = html + "This is <b>HTML</b> Response";
html = html + "</response>";
response.setContentType("text/html");
response.getWriter().write(html);
}else{
response.getWriter().write("default text Response type");
}
return null;
}
Yjunccefron-i1981 German Cirulis https://wakelet.com/wake/o9dhaVbsU45fQt6cLf8gw
ReplyDeletenabseiteti
brosinmesji-1989 Jill Williams https://www.studypaths.org/profile/daishawnaadeenah/profile
ReplyDeletelighsongcomli
randbaOcrisro1993 Carlos Ruiz
ReplyDeletecarnidoma