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

Ajax trong JQuery (P7) - Xử lý Ajax timeouts


Đôi khi, bạn có thể không muốn chờ một quá trình Ajax hoàn thành nếu nó diễn ra qua lâu, ví dụ: nguồn tài nguyên của bạn đang cố gắn lấy từ máy chủ không hề tồn tại

Bạn có thể thiết lập một thời gian chờ trong vài giây với thuộc tính timeout của hàm $.ajax(). Ví dụ, chỉ đợi trong vòng 10s
 
<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(){
$.ajax({
type: "POST",
url: "poster1.php",
data: {data: 1},
success: callback,
timeout: 10,
error: err
});
});

function callback(data, status)
{
$("div").text(data);
}

function err(xhr, reason, ex)
{
$("div").text(reason);
}
</script>
</head>

<body>
<h1>Using the jQuery $.post()function</h1>
Got this from the server: <div></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