HTML:
Javascript:
function stringify() {
let obj = new Object();
obj.gcode = $("#txt_gcode").val();
obj.gname = $("#txt_gname").val();
let array = [];
array.push(obj);
$.ajax({
type: "POST",
url: "ashx/testJson.ashx",
data: JSON.stringify(array),
success: function (e) {
readData();
alert(e);
}
});
/*alert(JSON.stringify(array));*/
}
testJson.ashx:
scratchDataContext sdc = new scratchDataContext();
static List _tj = new List();
public void ProcessRequest(HttpContext context)
{
string json = string.Empty;
//讀取前端傳來的 json 字串
using (var reader = new System.IO.StreamReader(context.Request.InputStream))
{
json = reader.ReadToEnd();
}
//將json 字串轉換成物件
if (!string.IsNullOrEmpty(json))
{
_tj = JsonConvert.DeserializeObject>(json);
}
gameClass gclass = new gameClass();
gclass.g_code = _tj.FirstOrDefault().gcode;
gclass.g_name = _tj.FirstOrDefault().gname;
sdc.gameClass.InsertOnSubmit(gclass);
sdc.SubmitChanges();
context.Response.ContentType = "text/plain";
context.Response.Write("成功新增一個項目");
}
public bool IsReusable
{
get
{
return false;
}
}
public class gc
{
public string gcode;
public string gname;
}