AngularJs Post 提交 改造

页面导航:首页 > 网络编程 > JavaScript > AngularJs Post 提交 改造

AngularJs Post 提交 改造

来源: 作者: 时间:2016-02-02 09:58 【

C 后台可通过Request[ "xxxx "] ToString()进行获取相对应的Form表单参数Html 代码ngPost提交测试 类型名: 职位名: 提交 AngularJs

C#后台可通过Request["xxxx"].ToString()进行获取相对应的Form表单参数

Html 代码

 




<script type="text/javascript" src="js/lib/angular.min.js" ></script>
类型名:
职位名:

AngularJs 控制器改造

 

        var app = angular.module("formApp", [], function ($httpProvider) {
            $httpProvider.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded;charset=utf-8";
            $httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
            var param = function (obj) {
                var query = "", name, value, fullSubName, subName, subValue, innerObj, i;
                for (name in obj) {
                    value = obj[name];
                    if (value instanceof Array) {
                        for (i = 0; i < value.length; ++i) {
                            subValue = value[i];
                            fullSubName = name + "[" + i + "]";
                            innerObj = {};
                            innerObj[fullSubName] = subValue;
                            query += param(innerObj) + "&";
                        }
                    } else if (value instanceof Object) {
                        for (subName in value) {
                            subValue = value[subName];
                            fullSubName = name + "[" + subName + "]";
                            innerObj = {};
                            innerObj[fullSubName] = subValue;
                            query += param(innerObj) + "&";
                        }
                    } else if (value !== undefined && value !== null) {
                        query += encodeURIComponent(name) + "=" + encodeURIComponent(value) + "&";
                    }
                }
                return query.length ? query.substr(0, query.length - 1) : query;
            };
            $httpProvider.defaults.transformRequest = [function (data) {
                return angular.isObject(data) && String(data) !== "[object File]" ? param(data) : data;
            }];
        });

调用方法:

 

        app.controller("formController", function ($scope, $http) {
            $scope.formData = {};
            $scope.processForm = function () {
                console.log("测试一下"+$scope.formData);
                $http.post("http://218.244.144.162:8094/Service/JobInterface.ashx",$scope.formData).success(function(data){
                    console.log(data);
                }).error(function(data){
                    console.log(data);
                    console.log("失败");
                });
            }
        });

C#后台编写,使用一般处理程序进行编写接口

 

 

        #region 全局变量
        bool dev = true;
        #endregion
        #region 处理HTTP请求
        public void ProcessRequest(HttpContext context)
        {
            context.Response.Cache.SetNoStore();//禁止自动缓存
            context.Response.Clear();
            context.Response.ContentType = "application/json";
            if (dev)
            {
                context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
                context.Response.Headers.Add("Access-Control-Allow-Headers", "X-Requested-With");
            }
            string action = "";
            string temp = "";
            try
            {
                action = context.Request["act"].ToString();
                //action = context.Request.Form["act"].ToString();
                temp = GetActionResult(action);
            }
            catch (Exception e)
            {
                temp = e.ToString();
            }
            //输出数据
            context.Response.Write(temp);
        }
        #endregion
        #region 返回操作结果
        /// 
        /// 返回操作结果
        /// 
        ///操作
        /// 
        private static string GetActionResult(string action)
        {
            string result = "无数据";
            switch (action)
            {
                case "GetJobByPage":
                    result = GetJobInfoByPage();
                    break;
                case "AddJob":
                    result = AddJob();
                    break;
            }
            return result;
        }
        #endregion

        #region 业务逻辑
        #region 添加
        /// 
        /// 添加职位
        /// 
        /// 
        private static string AddJob()
        {
            Job model = new Job();
            model.ID = 0;
            model.Name = HttpContext.Current.Request["name"];
            //model.Name = HttpContext.Current.Request.Form["name"].ToString();
            return JobController.AddJob(model);
        }
        #endregion


 

 

Tags:

文章评论

最 近 更 新
热 点 排 行
Js与CSS工具
代码转换工具

<