AngularJs Post提出改造
5154 ワード
C#バックグラウンドはRequest["xxxx"]を通過することができる.ToString()は、対応するFormフォームパラメータを取得します.
Htmlコード
AngularJsコントローラ
び し :
C#バックグラウンド 、 なプロセッサによるインタフェース
Htmlコード
ngPost
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://localhost:9901/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