AJAX 메서드 형식
$.ajax({
type: "POST"
, url: 요청페이지
, data: 파라미터 문자열
, success: 응답 처리 핸들러
, error: function(err) { alert('error : ' + err.status); } 에러 처리 핸들러
});
사용예
request.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
< html>
< head>
< meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
< title>JQuery Examples</title>
< script type="text/javascript" src="/scripts/jquery-1.5.1.min.js" ></script>
< style type="text/css">
</style>
<script type="text/javascript">
$().ready(function() {
$('#submit').click(function() {
var name = $('.uname').val();
var data = 'uname=' + name;
$.ajax({
type: "POST"
, url: "welcome.jsp"
, data: data
, success: handler
, error: function(err) { alert('error : ' + err.status); }
});
return false;
});
function handler(val) {
$('#message').html(val);
}
});
< /script>
< /head>
< body>
<form>
<label>Enter your Name</label>
<input type="text" name="uname" class="uname" /> <br />
<input type="submit" id="submit" />
</form>
<div id="message"></div>
< /body>
< /html>
welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
< %
request.setCharacterEncoding("UTF-8");
String name = (request.getParameter("uname") != null ) ? request.getParameter("uname") : "";
out.print("Welcome " + name);
%>