大家好,我这两天在调试一个Extjs form提交的时候,碰到了奇怪的问题,源代码如下:
Ext.onReady(function(){
var form = new Ext.form.FormPanel({
title:"用户注册",
width:600,
autoHeight:true,
labelAlign:"left",
labelWidth:60,
defaults:{xtype:"textfield"},
url:"admin/index.action",
listeners:{
"actioncomplete":function(_form,_action){
alert("OK");
},
"beforeaction":function(_form,_action){
alert("Submit");
}
},
items:[{
fieldLabel:"用户名",
name:"username"
},{
fieldLabel:"密码",
name:"password"
},{
fieldLabel:"重复密码"
},{
fieldLabel:"Email"
}],
renderTo:Ext.getBody(),
buttons:[{
text:"提交",
handler:function(){
form.getForm().submit();
}
},
{
text:"取消",
}
]
})
});
在提交时,事件beforeaction是可以触发的。但是,当服务器端的响应回来后,不能触发actioncomplete事件。不知是何原因。我还做过其它几个提交方法;如:
form.getForm().submit({
url:"http://localhost:8888/TestStruts2AndExtjs/admin/index.action",
method:"post",
success:function(form,action){
Ext.Msg.alert("友情提示","注册成功");
},
failure:function(form,action){
Ext.Msg.alert("友情提示","Sorry!注册失败");
}
});
都只能传数据到后台,都不能,在返回结果后,触发事件。
formPanel 表单提交返回的数据 需要一定规则的,
返回json数据需要有 sucess 属性表示 成功或者失败。
参考 Ext.form.Action.Submit 函数
[code="java"]success : function(response){
var result = this.processResponse(response);
if(result === true || result.success){
this.form.afterAction(this, true);
return;
}
if(result.errors){
this.form.markInvalid(result.errors);
this.failureType = Ext.form.Action.SERVER_INVALID;
}
this.form.afterAction(this, false);
},[/code]
所以跟平常ajax提交返回数据格式有点不一样的
{sucess:true,.....}
那可能是你后台返回的 json 格式数据出现问题了。
///
/// 生成指定格式返回数据
/// 格式为:{success:'操作状态',data:'操作结果信息'}
///
/// 操作成功为“true”,操作出错为“false”
/// 操作结果信息
///
private string WriteJsonResult(string success, string msg)
{
JsonWriter jw = new JsonWriter();
jw.WriteObjectStart();
jw.WritePropertyName("success");
jw.Write(success);
jw.WritePropertyName("data");
jw.Write(msg);
jw.WriteObjectEnd();
return jw.ToString();
}
}
我的数据uihou时这样输出的,但还是不对alert(action.result) 输出的始终是false