+ -
当前位置:首页 → 问答吧 → form表单提交 如何处理服务器端抛出的异常

form表单提交 如何处理服务器端抛出的异常

时间:2011-07-22

来源:互联网

使用Extjs 的FormPanel,提交表单到服务端,服务端的异常如何处理

我的前端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?

作者: xuexiaodong2009   发布时间: 2011-07-22

引用 1 楼 xuexiaodong2009 的回复:
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("'","") + "'}";
  }

作者: showbo   发布时间: 2011-07-22