1
0

ajax.html 4.7 KB

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