【学习备份】ajax添加小例子
实现目的:保存模板编号、模板名称到数据库
1、js引用
<script src="../../../../../../../js/jquery-1.10.2.min.js"></script> <script src="../../../../../../../js/jquery.query.js"></script>
js文件
1 <script type="text/javascript" language="javascript"> 2 debugger; 3 function after_saveTemp() { 4 if ($('#txtTicketno').val() == '') { 5 parent.message_alert("提示", "模板名称不能为空!", "info"); 6 } 7 else { 8 var TEMPDATA = { 9 id:$.query.get("id"), 10 templatename: $("#txtTicketno").val(), 11 }; 12 $.ajax({ 13 url: 'ashx/edit.ashx?act=temp&id="' + $.query.get("id") + '"&r=' + Math.random(), 14 contentType: "application/json;charset=utf-8", 15 data: JSON.stringify(TEMPDATA), 16 dataType: 'json', 17 type: 'POST', 18 success: function (data) { 19 debugger; 20 if (data.success == "true") { 21 parent.message_alert("提示", "模板保存成功!", "info"); 22 parent.closewin("over_step1_edit"); 23 } 24 else { 25 parent.message_alert("非常抱歉", "模板保存失败!请稍候重试", "info"); 26 } 27 } 28 }); 29 } 30 } 31 </script>
2、HTML文件
1 <div> 2 <table width="100%" border="0" cellpadding="0" cellspacing="1" class="formtable"> 3 <tr> 4 <td class="left">模板名称 5 </td> 6 <td class="right"> 7 <input id="txtTicketno" class="easyui-validatebox" required="true" style="width: 180px" /> 8 </td> 9 </tr> 10 </table> 11 </div> 12 <div class="edit_foot" style="margin-bottom:0px"> 13 <a class="easyui-linkbutton" iconcls="icon-ok" href="javascript:void(0)" onclick="after_saveTemp()">确定</a> 14 <a class="easyui-linkbutton" iconcls="icon-cancel" href="javascript:void(0)" onclick="javascript:parent.closewin(1,'over_step1_tempname')">取消</a> 15 </div>
3、ashx(一般处理程序)文件
using System.Web.Script.Serialization;
1 public void ProcessRequest(HttpContext context) 2 { 3 string act = p.ext_string(context.Request.Params["act"]); 4 switch (act) 5 { 6 case "temp": 7 SaveTempData(context); 8 break; 9 } 10 } 11 //添加修改 12 private void SaveTempData(HttpContext context) 13 { 14 Maticsoft.Model.WJX_SPECIALPLAN_BIRD modeltemp = new Maticsoft.Model.WJX_SPECIALPLAN_BIRD(); 15 using (System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream)) 16 { 17 string s = sr.ReadToEnd(); 18 modeltemp = new JavaScriptSerializer().Deserialize<Maticsoft.Model.WJX_SPECIALPLAN_BIRD>(s); 19 } 20 Maticsoft.BLL.WJX_SPECIALPLAN_BIRD blltemp = new Maticsoft.BLL.WJX_SPECIALPLAN_BIRD(); 21 bool rvalue = true; 22 try 23 { 24 Maticsoft.DBUtility.DbHelperOra.connectionString = p.getconfig("DB1"); 25 rvalue = blltemp.UpdateState(modeltemp); 26 if (rvalue) 27 { 28 context.Response.Write("{\"success\":\"true\"}"); 29 } 30 else 31 { 32 context.Response.Write("{\"success\":\"false\"}"); 33 } 34 } 35 catch 36 { 37 context.Response.Write("{\"success\":\"false\"}"); 38 } 39 40 context.Response.End(); 41 }