ajax数据接口 -- ZZZCMS/ZZZPHP模板帮助文档

[复制链接]
查看2420 | 回复0 | 2020-2-27 15:56 | 显示全部楼层 |阅读模式
仅限zzzphp版本
字段
名称
描述
table数据表从那个表调用数据
col数据字段调用哪些字段
where条件查询条件
num数量查询数量
page页码分页
order排序

jquery版本

  1. <script type="text/javascript">
  2. $(function () {         
  3.           $.ajax({
  4.                 type:"POST",
  5.                  url:"/form/?getjson",
  6.                   dataType:"json",
  7.                   data:{"table":"content","col":"cid,c_title,c_content","where":"c_sid=6","page":1,"order":'cid desc','num':10},
  8.         timeout: 1000,
  9.         cache: false,
  10.         beforeSend: LoadFunction, //加载执行方法  
  11.         error: erryFunction,  //错误执行方法  
  12.         success: succFunction //成功执行方法                  
  13.          });
  14.          function LoadFunction() {
  15.              $("#list").html('加载中...');
  16.          }
  17.          function erryFunction() {
  18.              alert("error");
  19.          }
  20.         function succFunction(data) {
  21.              $("#list").html('');
  22.              var json = eval(data); //数组      
  23.              $.each(json, function (index, item) {
  24.                  var title = json[index].c_title;
  25.                  var id = json[index].cid;
  26.                  var content = json[index].c_content;
  27.                  $("#list").html($("#list").html() + "<br>" + title + " - " + id + " - " +content+ "<br/>");
  28.              });
  29.          }
  30.      });
  31.     </script>
复制代码

js版本

  1. <script type="text/javascript">
  2.     //调用ajax函数
  3.     window.onload = function() {
  4.         ajax({
  5.             url: "/form/?getjson",
  6.             type: 'POST',
  7.             dataType: 'json',
  8.             data: {
  9.                 "table": "content",
  10.                 "col": "cid,c_title,c_content",
  11.                 "where": "c_sid=6",
  12.                 "page": 1,
  13.                 "order": 'cid desc',
  14.                 'num': 10
  15.             },
  16.             success: function(json, xml) {
  17.                 for (var i = 0, l = json.length; i < l; i++) {
  18.                     var content = document.getElementById("list").innerHTML + '<br>';
  19.                     document.getElementById("list").innerHTML = content + json[i].cid + " " + json[i].c_title;
  20.                 }
  21.             },
  22.             error: function(status) {
  23.                 alert(2222);
  24.             }
  25.         });
  26.     }
  27.     //创建ajax函数
  28.     function ajax() {
  29.         var ajaxData = {
  30.             type: arguments[0].type || "POST",
  31.             url: arguments[0].url || "",
  32.             async: arguments[0].async || "true",
  33.             data: arguments[0].data || null,
  34.             dataType: arguments[0].dataType || "text",
  35.             contentType: arguments[0].contentType || "application/x-www-form-urlencoded",
  36.             beforeSend: arguments[0].beforeSend || function() {},
  37.             success: arguments[0].success || function() {},
  38.             error: arguments[0].error || function() {}
  39.         }
  40.         ajaxData.beforeSend()
  41.         var xhr = createxmlHttpRequest();
  42.         xhr.responseType = ajaxData.dataType;
  43.         xhr.open(ajaxData.type, ajaxData.url, ajaxData.async);
  44.         xhr.setRequestHeader("Content-Type", ajaxData.contentType);
  45.         xhr.send(convertData(ajaxData.data));
  46.         xhr.onreadystatechange = function() {
  47.             if (xhr.readyState == 4) {
  48.                 if (xhr.status == 200) {
  49.                     ajaxData.success(xhr.response)
  50.                 } else {
  51.                     ajaxData.error()
  52.                 }
  53.             }
  54.         }
  55.     }

  56.     function createxmlHttpRequest() {
  57.         if (window.ActiveXObject) {
  58.             return new ActiveXObject("Microsoft.XMLHTTP");
  59.         } else if (window.XMLHttpRequest) {
  60.             return new XMLHttpRequest();
  61.         }
  62.     }

  63.     function convertData(data) {
  64.         if (typeof data === 'object') {
  65.             var convertResult = "";
  66.             for (var c in data) {
  67.                 convertResult += c + "=" + data[c] + "&";
  68.             }
  69.             convertResult = convertResult.substring(0, convertResult.length - 1)
  70.             return convertResult;
  71.         } else {
  72.             return data;
  73.         }
  74.     }
  75. </script>
复制代码

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

UID
1
贡献
387
丢币
38902
主题
4607
回帖
116
注册时间
2018-9-25
最后登录
2024-4-16