angular api请求简单封装get(),post()

api请求简单封装

  • api 简单封装
       $scope.api = {
                get: function (params, callback) {
                    $http({
                        url: params.api_url || default_url,
                        method: 'GET',
                        params: params
                    }).success(function (data) {
                        callback(data);
                    })
                },
                post: function (params, callback) {
                    $http({
                        url: params.api_url || default_url,
                        method: "POST",
                        data: params,
                        transformRequest: function (obj) {
                            var str = [];
                            for (var p in obj) {
                                str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                            }
                            return str.join("&");
                        },
                        headers: post_headers
                    }).success(function (data) {
                        callback(data);
                    })
                }
            };

example:

  • post方法
    $scope.reject_param={
        functioncode: 'reject_student_assess',
        team_id:team_id,
        product_id:resource_id,
        user_id: $scope.currentResource.id,
        assess_id:  $scope.assess_id,  
        advice:''
    }
    $scope.rejectStudentAssess=function(){   
        $scope.api.post(reject_param, function(data) {//调用封装的post api
            Tool.show_msg("驳回成功", "s_info", true);
        })
    }
  • get方法
    $scope.getUserResult = function(){
        var params = {
            functioncode: 'mark_assess_list',
            assess_id:  $scope.assess_id,
            team_id: team_id,
            user_id: $scope.currentResource.id
        };
        $scope.api.get(params, function(data) { //调用封装的get  api
            if(data.returncode=='000000'){
                $scope.user_info = data.record.user_list[0];
                $scope.question_list = data.record.question_list;
                $scope.user_total_score =  data.record.total_score;                                                                                                                                                                                                                         
                angular.forEach($scope.question_list, function(item) {
                    if(item.question_type=='subjective'){
                      item.reply = item.reply.substring(1, item.reply.length-1);
                    }
                    item.answer = [];
                    item.right_list_str = '';
                    $scope.checkIsSel(item);
                })
            }
        })
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值