使用jquery完成异步操作
-》开发文档提供的异步API
url:请求地址
type:请求方式,主要是get、post
data:{}:请求的数据
dataType:返回值的类型,主要有xml、text、json、script、html
success:function(data){...}成功的回调函数(4,200)
GetTime.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="js/jquery-1.7.1.min.js"></script>
<script>
$(function () {
$('#btnGetTime').click(function () {
$.ajax({
url: 'GetTime.ashx',
type: 'post',//'get',
data: {
'title': 'xlb'
},
dataType: 'html',
success: function (msg) {
$('#showTime').html(msg);
}
}); //$.get('GetTime.ashx',
// 'title=yg',
// function(msg) {
// $('#showTime').html(msg);
// }
// ); });
});
</script>
</head>
<body>
<input type="button" id="btnGetTime" value="获取时间" />
<div id="showTime"></div>
</body>
</html>
GetTime.ashx
public class GetTime : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html"; string title = context.Request["title"]; context.Response.Write("<h1>" + DateTime.Now.ToString() + "_" + title + "</h1>");
} public bool IsReusable
{
get
{
return false;
}
}
}