jquery ajax 简单范例

本文详细介绍了如何利用jQuery的AJAX和load方法从服务器获取并展示文本内容,包括同步和异步请求,以及处理请求状态的方法。

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

界面:
<script>
    $(document).ready(function () {
        $("#b01").click(function () {
            htmlobj = $.ajax({ url: "/Content/upFile/测试.txt", async: false });
            $("#div01").html(htmlobj.responseText);
        });
    });


    $(document).ready(function () {
        $("#test").load("/Content/upFile/测试2.txt #p1", function (responseTxt, statusTxt, xhr) {
            if (statusTxt == "success")
                alert("外部内容加载成功!" + xhr);
            if (statusTxt == "error")
                alert("Error: " + xhr.status + ": " + xhr.statusText);
        });
    });


    $(document).ready(function () {
        $("#getbutton").click(function () {
            $.get("/DownSet/index", function (data, status) {
                alert("数据:" + data + "\n状态:" + status);
            });
        });
    });


    $(document).ready(function () {
        $("#postbutton").click(function () {
            $.get("/CustomerInformation/getS", { par: "ww" }, function (data, status) {
                alert("数据:" + data + "\n状态:" + status);
                $("#postText").html(data);
            });
            
        });
    });




</script>


<div id="div04">
    <h2 id="postText">post方法加载文本后 会在这里显示</h2> 
    <input type="button" value="post方法获取" id="postbutton" />
</div>


<div id="div03">
    <h2 id="getText">get方法加载文本后 会在这里显示</h2> 
    <input type="button" value="get方法获取" id="getbutton" />
</div>


<div id="div02">
    <h2>jquery load方法加载的显示在这里</h2> 
    <h2 id="test">加载文本后 会在这里显示</h2> 
</div>


<div id="div01">
    <h2>加载文本后 会在这里显示</h2> 
</div>
<p>
    <input type="button" value="加载文本" id="b01"/>
</p>

后台:
public string getS(string par)
        {
            string content = "";
            if (par.Equals("ww"))
            {
                content = "cheng gong le";
            }
            else 
            {
                content = "shi bai le";
            }
            return content;
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值