Jquery Ajax请求实例

版权声明:转载原创文章请以超链接形式请注明原文章出处,尊重作者,尊重原创!


恰饭广告




<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://www.idaobin.com/js/jquery-3.2.1.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#ajax").click(function () {
                $.ajax({
                    type: 'Post',
                    url: 'test.aspx', //请求地址
                    data: {
                        op: "paging"
                    },
                    dataType: 'text', //返回类型
                    timeout: 1000, //超时时间,单位毫秒
                    beforeSend: function (XMLHttpRequest) { //开始请求
                        alert("正在发送请求");
                    },
                    success: function (data) { //请求成功
                        alert(data);
                    },
                    complete: function (XMLHttpRequest, textStatus) { //请求完成
                        if (textStatus == 'timeout') {
                            var xmlhttp = window.XMLHttpRequest ? new window.XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHttp");
                            xmlhttp.abort();
                            alert("请求超时");
                        }
                        else if (textStatus == "success") {
                            alert("请求成功");
                        }
                        else {
                            alert("请求失败");
                        }
                    },
                    error: function (XMLHttpRequest, textStatus) { //请求失败
                        if (textStatus == 'timeout') {
                            var xmlhttp = window.XMLHttpRequest ? new window.XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHttp");
                            xmlhttp.abort();
                            alert("请求超时");
                        } else if (textStatus=="error") {
                            alert("服务器内部错误");
                        }
                        else if(textStatus=="parsererror"){
                            alert("返回类型错误");
                        }
                    }
                });
            });
        });
    </script>
</head>

<body>
    <button id="ajax">ajax</button>
</body>

</html>

原文链接:https://www.idaobin.com/archives/1964.html

让我恰个饭吧.ヘ( ̄ω ̄ヘ)

支付宝 ——————- 微信
图片加载中图片加载中



恰饭广告

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

30 + = 39