app时如何写一个全局的ajax,,然后在每个页面都是用

来源:互联网 发布:阿里云 发票抬头 编辑:程序博客网 时间:2024/06/03 08:49

所有的访问的接口的前面是固定的,如果分明写在各个页面,可能修改的时候就得一个个的改。现在把它放在一个js里面,然后再各个页面引用。修改了这个所有的文件里面也是改的。所以就更加方便管理和修改。

alluse.js里面的内容: 

    app.server = "https://m2.jft365.cn/index.php/";

    app.photourl = "https://m2.jft365.cn";
    app.userphotourl = "https://m1.jft365.cn";
    app.photourl2 = "http://a.jft365.cn";


app.post= function(act, data, fun, err) {

        mui.ajax({

            url:app.server + act,

            data: data,
            dataType: 'json',
            type: 'post',
            timeout: 5000,
            success: function(json, s, xhr) {
                app.wait.close();
                if(json == null) {
                     console.log("json转换错误");
                    return  console.log(xhr.responseText);
                }
                if(json.status==1) {     //状态为1代表正确
                    fun(json.data || null, json);

                } 

                else {

                         mui.toast(json.msg);     //这里 json.msg也可能是json.info看后台在报错时给出的数据名称
                         console.log("ajax未处理的错误:" + json.msg);
                }
            },
            error: function(xhr, type, errorThrown) {
                app.wait.close();
                switch(type) {
                    case "abort":
                        return;
                    case "error":
                        mui.toast('网络错误,请稍后重试!');
                        console.error("服务器错误:" + errorThrown);
                        break;
                    case "timeout":
                        mui.toast('服务器连接超时,请重试!');
                        break;
                    case "parsererror":
                        mui.toast('返回数据格式不规范!');
                         console.log("ajax返回格式错误:");
                         console.log(xhr.responseText);
                        break;
                }
            }
        });

    };


app.wait = {
        show: function(msg) {
            plus.nativeUI.showWaiting(msg);
        },
        close: function() {
            if(window.plus) plus.nativeUI.closeWaiting();
        }
    };



其他页面

<script src="../alluse.js"></script>

<script>

  app.post("member/index",{id:goods_id,uid:user.id},function(db){

    //成功以后的操作,db是返回的值


  })



</script>



0 0
原创粉丝点击