知识卡片$http


与远程HTTP服务器交互时会用一个非常关键的服务-$http

1.普通$http请求


config 参数说明

method  {String} 请求方式e.g. "GET"."POST"
url {String} 请求的URL地址
params {key,value} 请求参数,将在URL上被拼接成?key=value
data {key,value} 数据,将被放入请求内发送至服务器
cache {boolean} 若为true,在http GET请求时采用默认的$http cache,否则使用$cacheFactory的实例
timeout {number} 设置超时时间
    
响应函数参数说明

data 响应体
status 相应的状态值
headers 获取getter的函数
config 请求中的config对象,同上第1点


某个controller.js:

angular.module('app.appCtr'[])
/*js文件名.功能模块名*/
.controller('appCtr.sidebar',
	function($scope,$http){
       // $http(config).success(function(data,status,headers,config){}).error(function(data,status,headers,config){});
	// config = {url:"login.do",method:"post",data:{name:"12346",pwd:"123"}}。 

var config={
	method : 'POST',
	url : url,
	data : {},
	headers : {
		'Content-Type' : 'application/x-www-form-urlencoded;charset=UTF-8'
	},
	transformRequest : function(obj) {
		var str = [];
		for ( var p in obj)
			str.push(encodeURIComponent(p)
			+ "="
			+ encodeURIComponent(encodeURIComponent(obj[p])));
		return str.join("&");
	},
	timeout : "120"
};

	$http(config)
	.success(function(data,status,headers,config){
		// 提示信息
		// 数据处理
		// console.log(data)

	})
	.error(function(data,status,headers,config){
		// 提示信息
		// 数据处理
		// console.log(data)

	});



	// end
};


2.封装 $http

var runService = function($http, $ionicLoading, $ionicPopup,
    url,//接口名
    data,//传送数据{}
){
    // 兼容处理
    if (typeof ($ionicPopup) == 'string')
    {
        errorFun = successFun;
        successFun = data;
        data = url;
        url = $ionicPopup;
        $ionicPopup = undefined;
        // 目的讲传入参数设置 undefined
    }

    if (navigator && navigator.connection && navigator.connection.type == 'none')
    {
        // 网络不可用
    }

    if ($ionicLoading)
    {
        // 处理中...
        ionicShowMsg($ionicLoading);

        
    }
    var config={
    method : 'POST',
    url : url,
    data : data,
    headers : {
        'Content-Type' : 'application/x-www-form-urlencoded;charset=UTF-8'
    },
    transformRequest : function(obj) {
        var str = [];
        for ( var p in obj)
            str.push(encodeURIComponent(p)
            + "="
            + encodeURIComponent(encodeURIComponent(obj[p])));z
        return str.join("&");
    },
    timeout : "120"
    };
// $http调用
    $http(config)
    .success(function(data,status,headers,config){

        // todo
        if ($ionicLoading){$ionicLoading.hide()}

        if(status == 200)
        {
            if(data.status=='200')
            {
                successFun.call(this, data, status);
            }else{
                // 返回接口错误原因
                ionicShowMsg($ionicLoading,data.message,1500);
            }

        }else{
            //数据获取失败
            ionicShowMsg($ionicLoading,"数据获取失败",1500);
        }

        if (status == 0)
        {
            //连接超时
            ionicShowMsg($ionicLoading,"连接超时",1500)

        }

    }).error(function(data,status,headers,config){

        // todo
        if ($ionicLoading){$ionicLoading.hide()}
        //网络异常
            ionicShowMsg($ionicLoading,"网络异常",1500)


    });


}


// ionicShowMsg 信息提示
var ionicShowMsg = function($ionicLoading,template,duration){

    // $ionicLoading,template,duration

        if ($ionicLoading)
        {
            $ionicLoading.show({
                if(!duration){

                    template:(!template || template=='')?"处理中...":template,
                })

            }else{
                $ionicLoading.show({
                    template:(!template || template=='')?"处理中...":template,
                    duration:duration,
                })
            }

        }
            
    };
var ionicAlertMsg = function($ionicPopup,template,okText){

    // $ionicLoading,template,duration
    //$ionicPopup,template,okText
        // template,okText
        if ($ionicPopup)
        {
            if(!template)
            {
                $ionicPopup.alert({
                    template : template,
                    okText : (!okText||okText=='')?:"确定":okText,
                    okType : "button-confirm",
                    cancelType : "button-cancel",
                    cssClass : "custom_popup",
                });

            }
            

        }
            
            
    };


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值