TypechoJoeTheme

IT技术分享

统计

AngularJS - HTTP请求(POST、GET)模板

2016-08-07
/
0 评论
/
782 阅读
/
正在检测是否收录...
08/07

一、依赖下载

本模板需要NProgress和Angular-MsgBox的支持,GIthub下载地址:
https://github.com/dunso/nprogress
https://github.com/dunso/Angular-MsgBox

二、模板代码

'use strict';
var app = angular.module('app', [
    'ngAnimate',
    'ngCookies',
    'ngStorage',
    'app.controllers',
    'msgbox'
])

angular.module('app.controllers', ['ngCookies'])
    .factory('Dunso', function ($http, $rootScope, $state) {
        var DUNSO_SERVER_URL = "https://dun.so/";
        return {
            //POST请求(通用AJAX方式)
            post_jquery: function (api, data, success, error) {
                var url = DUNSO_SERVER_URL + api;
                this.isDebug(url, data, 'POST');
                NProgress.start();
                $.ajax({
                    method: "POST",
                    url: url,
                    data: JSON.stringify(data),
                    contentType: "application/json",
                    dataType: 'json',
                    async: false,
                    success: function (response) {
                        NProgress.done();
                        success(response);
                    },
                    error: function (response) {
                        NProgress.done();
                        error(response);
                    },
                    timeout: function () {
                        NProgress.done();
                    }
                });
            },
            //POST请求(Angular方式)
            post: function (api, data, success, error) {
                var url = DUNSO_SERVER_URL + api;
                this.isDebug(url, data, 'POST');
                NProgress.start();
                var request = {
                    'method': 'POST',
                    'url': url,
                    'headers': {
                        'Content-Type': 'application/json'
                    },
                    'data': data
                }
                $http(request).success(function (response) {
                    if (response.err_code == undefined) {
                        this.notify('服务器繁忙,请稍后重试! ','error');
                    }
                    if (success != undefined) {
                        NProgress.done();
                        success(response);
                    }

                }).error(function (response) {
                    if (response.err_code != undefined) {
                        this.notify('网络不给力哟,请休息片刻再试试! ','warn');
                    }
                    if (error != undefined) {
                        NProgress.done();
                        error(response);
                    }
                });
            },
            //GET请求(Angular方式)
            get: function (api, success, error) {
                var url = DUNSO_SERVER_URL + api;
                this.isDebug(url,null,'GET')
                NProgress.start();
                $http.get(url).success(function (response) {
                    if (response.err_code != undefined) {
                        this.notify('服务器繁忙,请稍后重试! ','error');
                    }
                    if (success != undefined) {
                        NProgress.done();
                        success(res);
                    }
                }).error(function (response) {
                    if (response.err_code != undefined) {
                        this.notify('网络不给力哟,请休息片刻再试试! ','warn');
                    }
                    if (error != undefined) {
                        NProgress.done();
                        error(response);
                    }

                });
            },
            isDebug: function (url, data, method) {
                if ($rootScope.DEBUG) {
                    console.log(method + ':' + url);
                    console.log('Data: ' + JSON.stringify(data));
                }
                return;
            },
            notify:function(msg,type){
                $.notify({
                    message: msg
                }, {
                    placement: {
                        from: "top",
                        align: "center"
                    },
                    type: type
                });
            }
        };
    })
朗读
赞 · 0
版权属于:

IT技术分享

本文链接:

https://idunso.com/archives/2110/(转载时请注明本文出处及文章链接)