AngularJs Post submission Transformation
C # The background can obtain the corresponding Form parameters through Request ["xxxx"]. ToString ().
Html code
<Script type = "text/javascript" src = "js/lib/angular. min. js"> </script>
AngularJs controller Transformation
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; }]; });
Call method:
App. controller ("formController", function ($ scope, $ http) {$ scope. formData ={}; $ scope. processForm = function () {console. log ("test" + $ 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 ("failed ");});}});
C # write in the background and use the general processing program to write the interface
# Region global variable bool dev = true; # endregion # region processes the HTTP request public void ProcessRequest (HttpContext context) {context. response. cache. setNoStore (); // disable automatic context caching. 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 ();} // output data context. response. write (temp) ;}# endregion # region returns the operation result ////// Return the operation result //////Operation ///
Private static string GetActionResult (string action) {string result = "no data"; switch (action) {case "GetJobByPage": result = GetJobInfoByPage (); break; case "AddJob ": result = AddJob (); break;} return result ;}# endregion # region business logic # add region ////// Add a position //////
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