使用ajax,获取笑话。

这篇博客展示了如何使用JavaScript的jQuery库通过AJAX从API获取笑话并显示在网页上。用户点击‘点我有笑话’按钮后,内容_div会填充从API获取的3条笑话内容。涉及到的技术包括jQuery、AJAX、JSON数据解析以及前端页面动态更新。
摘要由CSDN通过智能技术生成
<body>
    <button class="btnJoke" style="height: 50px;width: 100px;background-color: rgb(23, 189, 240);">点我有笑话</button>
    <div class="content_div" style="background-color: burlywood;height: 600px;width: 500px;color: crimson;font-size: 20px;">
    </div>
</body>
<script>
    $(".btnJoke").on("click", function() {
        getContent();
    });

    function getContent() {
        $.ajax({
            type: "GET", //默认为get
            url: "https://autumnfish.cn/api/joke/list?num=3",
            async: "true",
            data: {
                name: function() {
                    return 1;
                }
            },
            //当dataType为script时,默认为false,
            //设置为false将不会从浏览器缓存中加载请求信息。在使用ajax进行文件上传的时候需要将其设为false
            cache: "true",
            //在一般的get、post的普通请求使用processData:false会导致请求出错,无法正常参数的绑定,
            //在FormData的情况进行文件的上传之类的就必须要设processData:false
            processData: "true",
            contentType: 'application/json;charset=utf-8',
            //beforeSend
            dataType: "json",
            success: function(data) {
                $(".content_div").html("");
                for (var i = 0; i < data.jokes.length; i++) {
                    $(".content_div").append(data.jokes[i]);
                    $(".content_div").append("</br></br>");
                }
            },
            error: function(ex) {
                console.log("获取图件列表失败");
            }
        });
    };
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值