我有form,提交方式如下:
form.submit({
url: '/postme',
waitMsg: 'Loading...',
method: 'POST',
success: function (form, action) {
console.log(action.response.responseText);
}
});
但是!回答的主要问题是我没有属性success: true
并且无法设置添加它。有没有办法在Ext.Ajax.request中制作类似callback
参数的内容
而不是success
?或者也许有一种方法告诉form.submit()它必须检测其他responsece参数的成功吗?
BTW:a不能使用Ext.Ajax.request,因为我必须从表单传递多部分数据(文件上传)。
提前致谢。
答案 0 :(得分:10)
API解释说您必须传递success: true
才能调用成功处理程序。这是submit
方法
@param {Object}选项 传递给操作的选项(有关详细信息,请参阅Ext.form.Basic.submit和Ext.form.Basic.doAction)
if (form.isValid()) {
// Submit the Ajax request and handle the response
form.submit({
success: function(form, action) {
Ext.Msg.alert('Success', action.result.message);
},
// If you don't pass success:true, it will always go here
failure: function(form, action) {
Ext.Msg.alert('Failed', action.result ? action.result.message : 'No response');
}
});
}
如果您想使用其他属性来指示错误,您应该查看http://docs.sencha.com/extjs/4.2.0/#!/api/Ext.form.Basic-cfg-errorReader,您可以配置errorReader以根据需要读取您的属性。
您还可以将成功和失败处理程序路由到同一个地方,并从处理程序
中确定它请务必设置standardSubmit:true
,以便不会使用AJAX提交
示例解决方案将使您的代码感觉不那么脏
Ext.define('ns.my.CustomReader', {
extend: 'Ext.data.reader.Reader',
alias: 'reader.ns-customreader',
read: function(xhr)
var resp = Ext.JSON.decode(response.responseText, true);
// Handle the case where response is not JSON too (unhandled server errror?)
return {success: resp ? !!resp.id : false};
}
});
然后,当您创建表单时,您可以使用
form : {
errorReader: 'ns-customreader'
}
我还注意到你没有返回记录,读者应该按照文档做的,可能你只是不需要它,但是满足界面来保持你的记录会很好代码符合Ext-JS
errorReader不一定是Reader的完整实现。它只需要实现一个read(xhr)函数,该函数返回一个具有以下结构的对象中的记录数组:
{ records: recordArray } // I think it needs success: boolean also.
答案 1 :(得分:0)
如果您不发送success: true
,那么显然会失败。因此,在类似于此的故障回调函数中处理您的参数,其中success
的替代方法为status
:
form.submit({
url: '/postme',
waitMsg: 'Loading...',
method: 'POST',
success: function (form, action) {
console.log(action.response.responseText);
},
failure: function(form, action) {
if (action.result.status == true) {
console.log('success!');
}
}
});
答案 2 :(得分:0)
除了@Juan Mendes的回答,我发现以下条件必须成立:
在服务器端
对于版本extjs-3.4.0
,情况也是如此请查看:Why success callback is not called in extjs form submission?
这为我节省了很多时间,我希望它可以帮助别人。