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

仙剑 2011-07-22 02:18:21
使用Extjs 的FormPanel,提交表单到服务端,服务端的异常如何处理

我的前端Form代码如下:
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();

后台代码:

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过程,不知怎么回事,请高手帮忙!
...全文
885 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
仙剑 2011-08-01
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 fanchuanzhidu 的回复:]

走failure得你的ajax过程出错了 要么超时 要么网络中断之类的 不关服务端的事
再说 服务端抛了异常 后边response语句还能执行?就算能执行 前台也是走success函数 也不会走到failure里
[/Quote]

可是也没有走Success过程,直接给我报错了
豆虫 2011-07-27
  • 打赏
  • 举报
回复
走failure得你的ajax过程出错了 要么超时 要么网络中断之类的 不关服务端的事
再说 服务端抛了异常 后边response语句还能执行?就算能执行 前台也是走success函数 也不会走到failure里
yashucn 2011-07-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 showbo 的回复:]
可能是生成的错误中包含了单引号,导致"{success:false,msg:'" + ex.ToString() + "'}" json格式出错,替换掉'试试

catch (Exception ex)
{
returnValue = "{success:false,msg:'" + ex.ToString().Rep……
[/Quote]

json应该支持单引号,倒是里面如果有双引号不可以,会报parse错误,例如【"<p name="dd"></p>"】
  • 打赏
  • 举报
回复
可能是生成的错误中包含了单引号,导致"{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("'","") + "'}";
}
仙剑 2011-07-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xuexiaodong2009 的回复:]
catch (Exception ex)
{
returnValue = "{success:false,msg:'" + ex.ToString() + "'}";
}
把异常都处理了能走Failure?
[/Quote]

我 success 返回的是 false

failure 不执行吗?
xuexiaodong2009 2011-07-22
  • 打赏
  • 举报
回复
catch (Exception ex)
{
returnValue = "{success:false,msg:'" + ex.ToString() + "'}";
}
把异常都处理了能走Failure?

52,792

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧