jquery ajax 的异步调

本文详细介绍了如何使用AJAX进行异步请求,包括GET和POST方法,并通过实例展示了服务器端处理异步请求的过程,实现了客户端与服务器之间的高效交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>ajax</title>
    <script src="jquery-latest.pack.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function(){
            //设置指示器
            $('#divIndicator').ajaxStart(function(){$(this).show()})
                            .ajaxSuccess(function(){$(this).hide()})
                            .ajaxError(function(msg){$(this).hide();alert(msg);});
            //ajax get 请求
            $('#btnGetCubeInGet').click(function(){
                var number = $('#txtNumber').val();
                $.get('ajaxServer.ashx?number='+number,function(result){
                    alert(result);
                });
            });
           
            //ajax post 提交
            $('#btnGetCubeInPost').click(function(){
                var number = $('#txtNumber').val();
                $.post('ajaxServer.ashx',{'number':number},function(re){
                    alert(re);
                });
            });
        });
    </script>
    <style type="text/css">
        .indicator
        {
            color: #FF0000;
            position: absolute;
            top: 0px;
            right: 0px;
            display: none;
        }
    </style>
</head>
<body>
    <div id="divIndicator" class="indicator">
        <img src="about_img.gif" />loading</div>
    plz input a number:<input id="txtNumber" />
    <input type="button" id="btnGetCubeInGet" value="Get cube(get)" />
    <input type="button" id="btnGetCubeInPost" value="Get cube(post)" />
</body>
</html>

 

所调请求的页面内容是:

<%@ WebHandler Language="C#" Class="ajaxServer" %>

using System;
using System.Web;

public class ajaxServer : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
       
        context.Response.ContentType = "text/plain";
        int number = 0;
        int.TryParse(context.Request.Params["number"], out number);
        context.Response.StatusCode = 200;
        context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        context.Response.Write(string.Format("{0} cubed is {1}", number, Math.Pow(number, 3)));
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值