kimi
2021-02-18 749a5510a9f014446a3cd6ba57b3cb0cc8148dc1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<!DOCTYPE html>
<html>
 
    <head>
        <meta charset="utf-8">
        <title>Hello MUI</title>
        <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
 
        <!--标准mui.css-->
        <link rel="stylesheet" href="../css/mui.min.css">
        <!--App自定义的css-->
        <!--<link rel="stylesheet" type="text/css" href="../css/app.css"/>-->
        <style>
             .mui-content-padded {
                padding: 10px;
            }
            body,body .mui-content {
                background-color: #fff !important;
            }
            code {
                word-wrap: break-word;
                word-break: normal;
                font-size: 90%;
                color: #c7254e;
                background-color: #f9f2f4;
                border-radius: 4px;
            }
        </style>
    </head>
 
    <body>
        <header class="mui-bar mui-bar-nav">
            <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
            <h1 class="mui-title">ajax(网络请求)</h1>
        </header>
        <div class="mui-content">
            <div class="mui-content-padded" style="padding-bottom: 50px;">
                <p style="text-indent: 22px;">
                    mui基于html5plus的<a href="http://www.dcloud.io/docs/api/zh_cn/xhr.shtml">XMLHttpRequest</a>,封装了常用的ajax函数,支持Get、Post请求方式, 支持返回json、xml、html、text、script数据类型;
                </p>
                <h4 class="mui-content-padded">发送请求:</h4>
                <div class="mui-input-group">
                    <div class="mui-input-row">
                        <label>type:</label>
                        <select id="method">
                            <option value="get">GET</option>
                            <option value="post">POST</option>
                        </select>
                    </div>
                    <div class="mui-input-row">
                        <label>dataType:</label>
                        <select id="dataType">
                            <option value="html">HTML</option>
                            <option value="json">JSON</option>
                            <option value="xml">XML</option>
                        </select>
                    </div>
                    <div class="mui-button-row">
                        <button type="button" id="confirm" class="mui-btn mui-btn-primary">发送请求</button>
                    </div>
                </div>
                <h4 class="mui-content-padded">获得响应:</h4>
                <code id="response"></code>
            </div>
        </div>
        <script src="../js/mui.min.js"></script>
        <script>
            (function($) {
                $.init({
                    swipeBack:true //启用右滑关闭功能
                });
                
                var network = true;
                if(mui.os.plus){
                    mui.plusReady(function () {
                        if(plus.networkinfo.getCurrentType()==plus.networkinfo.CONNECTION_NONE){
                            network = false;
                        }
                    });
                }
                
                var methodEl = document.getElementById("method");
                var dataTypeEl = document.getElementById("dataType");
                var respnoseEl = document.getElementById("response");
                //成功响应的回调函数
                var success = function(response) {
                    var dataType = dataTypeEl.value;
                    if (dataType === 'json') {
                        response = JSON.stringify(response);
                    } else if (dataType === 'xml') {
                        response = new XMLSerializer().serializeToString(response).replace(/</g, "&lt;").replace(/>/g, "&gt;");
                    }
                    respnoseEl.innerHTML = response;
                };
                //设置全局beforeSend
                $.ajaxSettings.beforeSend = function(xhr, setting) {
                    //beforeSend演示,也可在$.ajax({beforeSend:function(){}})中设置单个Ajax的beforeSend
                    console.log('beforeSend:::' + JSON.stringify(setting));
                };
                //设置全局complete
                $.ajaxSettings.complete = function(xhr, status) {
                    console.log('complete:::' + status);
                }
                var ajax = function() {
                    //利用RunJS的Echo Ajax功能测试
                    var url = 'https://service.dcloud.net.cn/ajax/echo/';
                    //请求方式,默认为Get;
                    var type = methodEl.value;
                    //预期服务器范围的数据类型
                    var dataType = dataTypeEl.value;
                    //发送数据
                    var data = {
                        name: "mui",
                        version: "pre-release",
                        author: "chb",
                        description: "最接近原生APP体验的高性能前端框架"
                    };
                    url = url + (dataType === 'html' ? 'text' : dataType);
                    respnoseEl.innerHTML = '正在请求中...';
                    if (type === 'get') {
                        if (dataType === 'json') {
                            $.getJSON(url, data, success);
                        } else {
                            $.get(url, data, success, dataType);
                        }
                    } else if (type === 'post') {
                        $.post(url, data, success, dataType);
                    }
                };
                //发送请求按钮的点击事件
                document.getElementById("confirm").addEventListener('tap', function() {
                    if(network){
                        ajax();
                    }else{
                        mui.toast("当前网络不给力,请稍后再试");
                    }
                });
                //点击描述中链接时,打开对应网页介绍;
                $('body').on('tap', 'a', function(e) {
                    var href = this.getAttribute('href');
                    if (href) {
                        if (window.plus) {
                            plus.runtime.openURL(href);
                        } else {
                            location.href = href;
                        }
                    }
                });
            })(mui);
        </script>
    </body>
 
</html>