前台js:
var app = angular.module("bonusbatchapp", []);
app.controller("customersController",function($scope,$http){<span style="font-family: Arial, Helvetica, sans-serif;">//页面加载之后就显示数据</span>
$http.get("/temp/datajson.txt").success(function(Data) {
$scope.members = Data.members;
}).error(function(data,status){
alert("error");
});
<span style="font-family: Arial, Helvetica, sans-serif;">//点击之后,post json数据</span>
$scope.postBatchBonusOk = function(){
$http.post("ashx/BonusBatch.ashx",$scope.members).success(function (Data){
}).error(function (data,status){
alert("error2");
});
};
});
后台(因为.aspx对应的cs文件主要是对服务器控件进行响应,如果前台控件是以html控件为主的话,则后台使用.ashx,类似于java的servlet):
using System;
using System.Web;
using Newtonsoft.Json;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
//只是一段测试代码
public class BonusBatch : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
Object dicParameter = GetParameter(context);
}
public bool IsReusable {
get {
return false;
}
}
private Object GetParameter(HttpContext context)
{
System.IO.StreamReader reader = new System.IO.StreamReader(context.Request.InputStream);
String strJson = HttpUtility.UrlDecode(reader.ReadToEnd());
if (!String.IsNullOrEmpty(strJson))
{
JArray ja = (JArray)JsonConvert.DeserializeObject(strJson);
String name = "";
String bonus = "";
String reason = "";
for (int i = 0; i < ja.Count; i++)
{
name = ja[i]["Name"].ToString();
bonus = ja[i]["Bonus"].ToString();
reason = ja[i]["Reason"].ToString();
}
}
Object obj = new Object();
return obj;
}
}