标签:angularjs javascript js post 表单
C#后台可通过Request["xxxx"].ToString()进行获取相对应的Form表单参数
Html 代码
<!DOCTYPE html> <html ng-app="formApp"> <head> <meta charset="utf-8"> <title>ngPost提交测试</title> <script type="text/javascript" src="js/lib/angular.min.js" ></script> </head> <body > <form name="form1" ng-controller="formController" method="post"> 类型名:<input type="text" name="act" ng-model="formData.act" /><br /> 职位名:<input type="text" name="name" ng-model="formData.name"/><br /> <button ng-click="processForm()" ng-disabled="!form1.$valid">提交</button> </form> </body> </html>
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("失败"); }); } });
#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 返回操作结果 /// <summary> /// 返回操作结果 /// </summary> /// <param name="action">操作</param> /// <returns></returns> 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 添加 /// <summary> /// 添加职位 /// </summary> /// <returns></returns> 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
标签:angularjs javascript js post 表单
原文地址:http://blog.csdn.net/jianxuanbing/article/details/45045645