Chủ Nhật, 17 tháng 10, 2010

Ajax trong JQuery (P9) - Xử lý toàn diện những sự kiện Ajax


Bạn có thể xử lý những sự kiện Ajax như một sự kiện thành công hoặc có lỗi cục bộ trong hàm $.ajax(), nhưng jQuery cũng cung cấp những chức năng để kết nối đến hàm phản hồi để bất kỳ sự kiện toàn cục nào đó cũng được xử lý tất cả qua Ajax của bạn. Những hàm xử lý toàn diện như  AjaxStart (), AjaxSend (), AjaxSuccess (), AjaxError (), AjaxComplete (), và AjaxStop ().
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="js/jquery-1.4.js" type="text/javascript"></script>
<script>
$(document).ready(function( ){
$("#starting").hide( );
$("#sending").hide( );
$("#success").hide( );
$("#complete").hide( );
$("#starting").bind("ajaxStart",
function( ){
$(this).show( );
});
$("#sending").bind("ajaxSend",
function( ){
$(this).show( );
});
$("#success").bind("ajaxSuccess",
function( ){
$(this).show( );
});
("#complete").bind("ajaxComplete",
function( ){
$(this).show( );
});
$.ajax({
type: "GET",
url: "poster.php",
data: {data: 1},
success: callback
});
});
function callback(data, status)
{
$("#results").text(data);
}
</script>
</head>

<body>
<h1>Handling Ajax events</h1>
<fiv id="starting">Starting...</div>
<fiv id="sending">Sending...</div>
<fiv id="success">Successful...</div>
<fiv id="complete">Complete...</div>
Got this in response: <div id="results"></div>
</body>
</html>

Tập tin poster.php
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
if ($_POST["data"] == "1") {
echo 'You sent the server a value of 1';
}

if ($_POST["data"] == "2") {
echo 'You sent the server a value of 2';
}
?>

</body>
</html>
(Nguồn: ZendVN group - www.zend.vn)

Không có nhận xét nào:

Đăng nhận xét

Girls Generation - Korean