form表单提交 如何处理服务器端抛出的异常
时间:2011-07-22
来源:互联网
使用Extjs 的FormPanel,提交表单到服务端,服务端的异常如何处理
我的前端Form代码如下:
JScript code
后台代码:
C# code
当数据库连接不成功时会发生异常,然后前台就会报出 js 错误。 我是想在前台提示服务器抛出的异常。
并没有走Failure过程,不知怎么回事,请高手帮忙!
我的前端Form代码如下:
JScript code
var win = Ext.create('Ext.window.Window', { //创建选择条件窗口 title: '条件选择', layout: 'fit', items: Ext.create('Ext.form.Panel', { border: 0, baseCls: 'x-plain', url: 'SPPage.aspx', bodyStyle: 'padding:20px 20px 10px 20px', items: [{ xtype: 'textfield', name: 'report_name', value: record.get('report_name'), hidden: true }, { xtype: 'textfield', name: 'report_path', value: record.get('report_path'), hidden: true }, { xtype: 'datefield', name: 'start_time', fieldLabel: '开始日期', format: 'Y-m-d', value: new Date(), width: 300, height: 30 }, { xtype: 'datefield', name: 'end_time', fieldLabel: '结束日期', format: 'Y-m-d', value: new Date(), height: 30, width: 300 }, { xtype: 'combobox', name: 'shifts', fieldLabel: 'Select Shift', multiSelect: true, editable: false, store: shiftstore, displayField: 'shift', valueField: 'id', height: 30, width: 300, queryMode: 'local' }, { xtype: 'combobox', name: 'skills', fieldLabel: 'Select Skill', multiSelect: true, store: skillstore, editable: false, width: 300, height: 30, displayField: 'skill_name', valueField: 'id', queryMode: 'local' }], bbar: ['->', { xtype: 'button', method:'post', text: '提交', handler: function () { var thisform = this.up('form').getForm(); if (thisform.isValid()) { thisform.submit({ success: function (form, action) { win.close(); //Then OpenTab //openTab(record); }, failure: function (form, action) { alert('提交数据错误!'); } }); } } }, { xtype: 'button', text: '取消', handler: function () { win.close(); } }, '->'] }) }); win.show();
后台代码:
C# code
public partial class SPPage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string returnValue = string.Empty; try { string redirectUrl = string.Empty; //DateTime start_time = DateTime.Parse(Request.Form["start_time"]); //DateTime end_time = DateTime.Parse(Request.Form["end_time"]); string start_time = Request.Form["start_time"]; string end_time = Request.Form["end_time"]; string shifts = Request.Form["shifts"]; string skills = Request.Form["skills"]; string report_name = Request.Form["report_name"]; string report_path = Request.Form["report_path"]; report_path = report_path.Replace('*', '\\'); ReportDocument repDoc = new ReportDocument(); repDoc.Load(report_path); string spNameValue = ""; spNameValue = (repDoc.DataDefinition.FormulaFields["spName"].Text); string spName = spNameValue.Substring(1, spNameValue.Length - 2); string strCon = ConfigurationManager.ConnectionStrings["UMPDBContext1"].ConnectionString; SqlConnection objCon = new SqlConnection(strCon); try { objCon.Open(); SqlCommand objCmd = new SqlCommand(); objCmd.CommandType = System.Data.CommandType.StoredProcedure; objCmd.CommandText = spName; objCmd.Connection = objCon; SqlParameter[] paramiters = new SqlParameter[5]{ new SqlParameter("@StartTime",start_time), new SqlParameter("@EndTime",end_time), new SqlParameter("@Shifts",""), new SqlParameter("@Skills",""), new SqlParameter("@UserVair","") }; foreach (SqlParameter _param in paramiters) { objCmd.Parameters.Add(_param); } SqlDataAdapter dapt = new SqlDataAdapter(objCmd); System.Data.DataSet ds = new System.Data.DataSet(); dapt.Fill(ds); Session["ds"] = ds; returnValue = "{success:true}"; } catch (Exception ex) { returnValue = "{success:false,msg:'" + ex.ToString() + "'}"; } //returnValue = "{success:true}"; } catch (Exception ex) { returnValue = "{success:false,msg:'" + ex.ToString() + "'}"; } //returnValue = "{success:false,msg:'sjfis'}"; Response.Write(returnValue); Response.End(); } }
当数据库连接不成功时会发生异常,然后前台就会报出 js 错误。 我是想在前台提示服务器抛出的异常。
并没有走Failure过程,不知怎么回事,请高手帮忙!
作者: jian200801 发布时间: 2011-07-22
catch (Exception ex)
{
returnValue = "{success:false,msg:'" + ex.ToString() + "'}";
}
把异常都处理了能走Failure?
{
returnValue = "{success:false,msg:'" + ex.ToString() + "'}";
}
把异常都处理了能走Failure?
作者: xuexiaodong2009 发布时间: 2011-07-22
引用 1 楼 xuexiaodong2009 的回复:
catch (Exception ex)
{
returnValue = "{success:false,msg:'" + ex.ToString() + "'}";
}
把异常都处理了能走Failure?
catch (Exception ex)
{
returnValue = "{success:false,msg:'" + ex.ToString() + "'}";
}
把异常都处理了能走Failure?
我 success 返回的是 false
failure 不执行吗?
作者: jian200801 发布时间: 2011-07-22
可能是生成的错误中包含了单引号,导致"{success:false,msg:'" + ex.ToString() + "'}" json格式出错,替换掉'试试
catch (Exception ex)
{
returnValue = "{success:false,msg:'" + ex.ToString().Replace("'","") + "'}";
}
//returnValue = "{success:true}";
}
catch (Exception ex)
{
returnValue = "{success:false,msg:'" + ex.ToString().Replace("'","") + "'}";
}
catch (Exception ex)
{
returnValue = "{success:false,msg:'" + ex.ToString().Replace("'","") + "'}";
}
//returnValue = "{success:true}";
}
catch (Exception ex)
{
returnValue = "{success:false,msg:'" + ex.ToString().Replace("'","") + "'}";
}
作者: showbo 发布时间: 2011-07-22
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28