AngularJs Post submission Transformation

Source: Internet
Author: User

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


 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.