`
huangyongxing310
  • 浏览: 490434 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

Http请求封装

 
阅读更多
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <script src="angular.min.js"></script>
    <script src="jquery.min.js"></script>
</head>
<body ng-app="myApp">

<div ng-controller="myController">
</div>

</body>

<script>
    var app = angular.module('myApp', []);
    //定义服务
    app.service('commonFun', function ($http) {
        this.httpPost = function (url, dataObj, successFun, errorFun) {
            $http({
                method: 'POST',
                url: url,
                data: dataObj,
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                transformRequest: function (data) {
                    var str = '';
                    for (var i in data) {
                        str += i + '=' + data[i] + '&';
                    }
                    return str.substring(0, str.length - 1);
                }
            }).then(function successCallback(response) {
                if (successFun == null) {
                    alert("请求数据成功!")
                }
                else {
                    successFun(response);
                }
            }, function errorCallback(response) {
                //alert("getMenuListByMenuNameInRegex 请求数据错误!");
                if (errorFun == null) {
                    alert("url 请求数据错误!")
                } else {
                    errorFun(response);
                }
            });
        };

        this.httpPostFile = function (url, dataObj, successFun, errorFun) {
            var fd = new FormData();
            for (var i in dataObj) {
                fd.append(i, dataObj[i]);
            }
            $http({
                method: 'POST',
                url: url,
                data: fd,
                headers: {'Content-Type': undefined},
                transformRequest: angular.identity
            }).then(function successCallback(response) {
                if (successFun == null) {
                    alert("请求数据成功!")
                }
                else {
                    successFun(response);
                }
            }, function errorCallback(response) {
                //alert("getMenuListByMenuNameInRegex 请求数据错误!");
                if (errorFun == null) {
                    alert("url 请求数据错误!")
                } else {
                    errorFun(response);
                }
            });
        };

        this.httpGet = function (url, successFun, errorFun) {
            $http({
                method: 'GET',
                url: url
            }).then(function successCallback(response) {
                if (successFun == null) {
                    alert("请求数据成功!")
                }
                else {
                    successFun(response);
                }
            }, function errorCallback(response) {
                if (errorFun == null) {
                    alert("url 请求数据错误!")
                } else {
                    errorFun(response);
                }
            });
        };

        this.isEmptyObject = function (e) {
            var t;
            for (t in e) {
                return !1;
            }
            return !0
        };

        this.scollToElement = function (selector) {
            console.info("selector == " + selector);
            var elementVar = $(selector);
            var num = elementVar.length;
            console.info("num == " + num);
            console.info("elementVar == " + elementVar);
            var elementHeight = elementVar.outerHeight(true);
            console.info("elementHeight == " + elementHeight);
            //var elementOffsetTop = elementVar.offsetTop;
            var elementOffsetTop = elementVar.offset().top;
            console.info("elementOffsetTop == " + elementOffsetTop);
            $(document).scrollTop(elementOffsetTop + elementHeight);
        }
    });

    app.controller('myController', function ($scope, commonFun) {
        function getMenuListAllSuccessFun(response) {
            var errorCode = response.data.errorCode;
            console.info("errorCode == " + errorCode);
            console.info("data == " + response.data);
        }

        function getMenuListAll() {
            var url = 'http://119.23.31.212/common/CookerRecipeController/getMenuList';
            commonFun.httpGet(url, getMenuListAllSuccessFun);
        }

        function getMenuContentSuccessFun(response) {
            var errorCode = response.data.errorCode;
            console.info("errorCode == " + errorCode);
            console.info("data == " + response.data);
        }

        function getMenuContent(menuId) {
            var url = 'http://119.23.31.212/common/CookerRecipeController/getCookerRecipeByMenuId';
            var data = {menuId: menuId};
            commonFun.httpPost(url, data, getMenuContentSuccessFun);
        }

        function getMenuContent2(menuId) {
            var url = 'http://119.23.31.212/common/CookerRecipeController/getCookerRecipeByMenuId';
            var data = {menuId: menuId};
            commonFun.httpPostFile(url, data, getMenuContentSuccessFun);
        }

        getMenuListAll();
        getMenuContent(30000);
        getMenuContent2(30000);
    });
</script>
</html>
分享到:
评论

相关推荐

    c#Http请求封装类库.rar

    本压缩包"**c#Http请求封装类库.rar**"提供了一个方便的C# HTTP请求封装库,支持GET和POST方法,特别的是,它还能无视HTTPS证书,这对于开发、测试阶段非常有用,但请注意在生产环境中应谨慎使用,因为忽视证书验证...

    http请求封装

    封装了一下java 的http请求工具类要求jdk版本在1.8及以上

    微信小程序 http请求封装详解及实例代码

    微信小程序 http请求封装 示例代码 wx.request({ url: 'test.php', //仅为示例,并非真实的接口地址 data: { x: '' , y: '' }, method:'POST', header: { 'content-type': 'application/json' }, ...

    C# http Get/POST请求封装类

    在C#中,我们可以使用`System.Net.Http`命名空间下的HttpClient类来发送HTTP请求。以下是一个基础的`HttpHelper.cs`类的概览: ```csharp using System; using System.Net.Http; using System.Threading.Tasks; ...

    uniapp-脚手架(uview2.0+请求封装+vuex)

    【uniapp-脚手架(uview2.0+请求封装+vuex)】是一个基于uni-app框架构建的应用程序开发工具,结合了Uview2.0 UI库、请求封装技术和Vuex状态管理,为开发者提供了一个高效、便捷的开发环境。 **uni-app** 是一个由...

    php实现的http请求封装示例

    本文实例讲述了php的http请求封装。分享给大家供大家参考,具体如下: /** * 发送HTTP请求方法,目前只支持CURL发送请求 * @param string $url 请求URL * @param array $params 请求参数 * @param string $method ...

    c# http请求webapi接口封装类

    c# http请求webapi接口封装类

    自己封装的C#实现HTTP请求的动态链接库,dll

    标题中的“自己封装的C#实现HTTP请求的动态链接库,dll”指的是使用C#编程语言编写的HTTP客户端库,该库被封装成一个动态链接库(DLL)文件,供其他应用程序调用,以实现HTTP协议的网络通信功能。DLL是一种可重用的...

    php中http请求封装HttpClient精华中的经典代码

    HttpClient是一种常见的工具,用于封装HTTP请求,使得开发者可以方便地发送GET、POST等不同类型的请求。下面我们将深入探讨PHP中HttpClient的经典实现,以及如何通过封装来优化我们的代码。 首先,HttpClient的封装...

    封装好的HTTP请求类,基于QT

    本篇将详细探讨基于QT封装好的HTTP请求类的相关知识点。 首先,QT库中的网络模块提供了QNetworkAccessManager类,它是进行HTTP和FTP请求的核心。通过这个类,开发者可以方便地发起HTTP GET、POST等请求,并处理响应...

    Go-go-http自动记录cookie模拟请求http封装

    本篇文章将详细讲解如何利用Go的`net/http`包实现带有Cookie的HTTP请求封装,以及如何进行网页抓取。 首先,我们需要了解Cookie的基本概念。Cookie是一种小型文本文件,由服务器发送到用户的浏览器,并由浏览器存储...

    Android AndBase框架使用封装好的函数完成Http请求(三)

    总的来说,AndBase框架的Http请求封装提供了一种简单易用的方式,适合快速开发。然而,对于网络请求的特定需求,如高并发、低延迟,Volley可能是更好的选择。开发者可以根据项目需求和个人喜好来决定使用哪个框架。...

    网络Http请求的完整封装

    本项目"网络Http请求的完整封装"旨在提供一个全面的解决方案,方便开发者快速、高效地处理各种HTTP请求,包括GET和POST,以及图片下载功能。同时,它还包含了网络请求的进度显示和跨线程通信机制,极大地提高了用户...

    QT C++ http get、post 同步异步请求

    GET请求是最常见的HTTP请求类型,用于从服务器获取资源。在QT中,可以通过调用QNetworkAccessManager的get()函数实现。同步GET请求会阻塞当前线程,直到服务器响应返回;而异步GET请求则不会阻塞,它会在后台运行,...

    【鸿蒙版axios请求封装包】

    因此,"鸿蒙版axios请求封装包"是为了满足开发者在HarmonyOS环境下使用类似`axios`的API进行网络请求的需求而创建的。 这个封装包的目标是将`axios`的功能与HarmonyOS的特性相结合,提供一套简洁、高效且易于使用的...

    C# http GetPOST请求封装类

    创建一个名为`HttpClientHelper`的静态类,这个类将作为我们的HTTP请求封装: ```csharp using System; using System.IO; using System.Net.Http; using System.Threading.Tasks; public static class HttpClient...

    android 网络请求封装

    以下是一些关于“Android网络请求封装”的详细知识点: 1. **异步网络请求**: - Android中的网络操作必须在非UI线程中执行,否则会导致ANR(Application Not Responding)错误。因此,通常使用AsyncTask、Handler...

    swift-iOS轻量级http请求封装基于AFNetworking3.xYYCache

    轻量级http请求,基于封装AFN3.x YYCache, 接口设计上,通过block返回创建好的请求模型对象,调用者只有对非默认值属性进行修改即可,接口调用完成也只通过一个block来回调,具体情况通过响应模型对象来解析

Global site tag (gtag.js) - Google Analytics