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);
                })
            }
        })
    }
展开阅读全文

没有更多推荐了,返回首页