<%@ page language=
"java"
contentType=
"text/html; charset=ISO-8859-1"
pageEncoding=
"ISO-8859-1"
%>
<%@ page import=
"java.io.*"
%>
<html>
<head>
<meta http-equiv=
"Content-Type"
content=
"text/html; charset=ISO-8859-1"
>
<meta name=
"robots"
content=
"noindex,nofollow"
/>
<title>Programmers sample guide, help is on the way</title>
<link rel=
"stylesheet"
href=
"/resources/themes/master.css"
type=
"text/css"
/>
<script src=
"/resources/scripts/mysamplecode.js"
type=
"text/javascript"
></script>
<script type=
"text/javascript"
>
$(document).ready(
function
() {
$(
"#myAjaxRequestForm"
).submit(
function
(e){
e.preventDefault();
});
$(
"#myButton"
).click(
function
(e){
dataString = $(
"#myAjaxRequestForm"
).serialize();
$.getJSON(
"../CountryInfo"
, dataString,
function
( data, textStatus, jqXHR) {
if
(data.success){
$(
"#ajaxResponse"
).html(
""
);
$(
"#ajaxResponse"
).append(
"<b>Country Code:</b> "
+ data.countryInfo.code +
"<br/>"
);
$(
"#ajaxResponse"
).append(
"<b>Country Name:</b> "
+ data.countryInfo.name +
"<br/>"
);
$(
"#ajaxResponse"
).append(
"<b>Continent:</b> "
+ data.countryInfo.continent +
"<br/>"
);
$(
"#ajaxResponse"
).append(
"<b>Region:</b> "
+ data.countryInfo.region +
"<br/>"
);
$(
"#ajaxResponse"
).append(
"<b>Life Expectancy:</b> "
+ data.countryInfo.lifeExpectancy +
"<br/>"
);
$(
"#ajaxResponse"
).append(
"<b>GNP:</b> "
+ data.countryInfo.gnp +
"<br/>"
);
}
else
{
$(
"#ajaxResponse"
).html(
"<div><b>Country code in Invalid!</b></div>"
);
}
})
.error(
function
(jqXHR, textStatus, errorThrown){
$(
"#ajaxResponse"
).html(jqXHR.responseText);
})
.beforeSend(
function
(jqXHR, settings){
settings.data +=
"&dummyData=whatever"
;
$(
'#myButton'
).attr(
"disabled"
,
true
);
})
.complete(
function
(jqXHR, textStatus){
$(
'#myButton'
).attr(
"disabled"
,
false
);
});
});
});
</script>
</head>
<body>
<div id=
"allContent"
>
<div id=
"myExample"
>
<form id=
"myAjaxRequestForm"
>
<fieldset>
<legend>jQuery getJSON() example using Java Servlets and MySQL database</legend>
<p>
<label
for
=
"countryCode"
>Country Code:</label><br />
<input id=
"countryCode"
type=
"text"
name=
"countryCode"
/>
</p>
<p>
<input id=
"myButton"
type=
"button"
value=
"Submit"
/>
</p>
</fieldset>
</form>
<div id=
"anotherSection"
>
<fieldset>
<legend>Response from jQuery getJSON() Request</legend>
<div id=
"ajaxResponse"
></div>
</fieldset>
</div>
</div>
</div>
</body>
</html>
No comments:
Post a Comment