zoukankan      html  css  js  c++  java
  • ajax二次封装

    /**
    *@description Ajax请求二次封装 带loading窗口
    */
    function Ajax(options) {

    var wait_ico;
    if(plus != undefined && plus != null) {
    wait_ico = plus.nativeUI.showWaiting();
    }
    mui.ajax('http://' + GetServerInfo() + ((options.url || "").indexOf('/') == 0 ? (options.url || "") : ('/' + options.url || "/")), {
    data: options.data || "",
    dataType: 'json', //服务器返回json格式数据
    type: options.type || 'get', //HTTP请求类型
    timeout: options.timeout || 10000, //超时时间设置为10秒;
    headers: options.headers || "",
    crossDomain:true,
    success: function(data) {
    wait_ico.close();
    console.log(JSON.stringify(data));
    //data = data;
    //console.log(data.IsError)
    if(data.success) {
    if(options.success != null || options.success != undefined) {
    options.success.call(null, data);
    }
    } else {
    plus.nativeUI.toast(data.msg);
    }
    },
    error: function(xhr, type, errorThrown) {
    console.log(123);
    if(wait_ico != null && wait_ico != undefined) {
    wait_ico.close();
    }

    if(type == "timeout") {
    plus.nativeUI.toast("网络链接超时...");
    } else {
    plus.nativeUI.toast("数据加载异常,请重试");
    }

    if(options.error != undefined && options.error != null) {
    options.error.call(null, xhr, type, errorThrown)
    }
    }
    });

    }

    /**
    *@description Ajax请求二次封装 无loading窗口
    */
    function AjaxAnsyc(options) {
    mui.ajax('http://' + GetServerInfo() + ((options.url || "").indexOf('/') == 0 ? (options.url || "") : ('/' + options.url || "/")), {
    data: options.data || "",
    dataType: 'json', //服务器返回json格式数据
    type: options.type || 'get', //HTTP请求类型
    timeout: options.timeout || 10000, //超时时间设置为10秒;
    success: function(data) {
    if(data.success) {
    if(options.success != null || options.success != undefined) {
    options.success.call(null, data.result);
    }
    } else {
    plus.nativeUI.toast(data.msg);
    }
    },
    error: function(xhr, type, errorThrown) {
    if(type == "timeout") {
    plus.nativeUI.toast("网络链接超时");
    } else {
    plus.nativeUI.toast("数据加载异常,请重试");
    }
    if(options.error != undefined && options.error != null) {
    options.error.call(null, xhr, type, errorThrown)
    }
    }
    });

    }

  • 相关阅读:
    nodejs express搭建一个网站整理
    nodejs http post 请求带参数
    express respond.send 和 end的区别
    .net程序员做的第一个安卓APP
    angularjs ui-grid如何动态设置行高
    错误处理(Operation Result)方法
    jquery validation yyyy-MM-dd格式日期在ie中无法验证通过
    PAT (Basic Level) Practise (中文)- 1010. 一元多项式求导 (25)
    PAT (Basic Level) Practise (中文)- 1007. 素数对猜想 (20)
    PAT (Basic Level) Practise (中文)- 1012. 数字分类 (20)
  • 原文地址:https://www.cnblogs.com/daimaxuejia/p/13410300.html
Copyright © 2011-2022 走看看