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)
    }
    }
    });

    }

  • 相关阅读:
    主函数main
    static关键字
    this关键字
    构造函数
    封装
    匿名对象
    java基础积累
    JAVA相关知识复习
    ORACLE数据库表空间查询
    两个日期的时间差
  • 原文地址:https://www.cnblogs.com/daimaxuejia/p/13410300.html
Copyright © 2011-2022 走看看