ExtJS 4 Form.submit()没有成功:回答是真的

And*_*ich 4 javascript extjs extjs4

我有一个表单,提交方式如下:

form.submit({
                url: '/postme',
                waitMsg: 'Loading...',
                method: 'POST',
                success: function (form, action) {
                   console.log(action.response.responseText);                                   
                }
            });
Run Code Online (Sandbox Code Playgroud)

但!回答的主要问题是我没有财产success: true,无法设法添加它.有没有办法callbackExt.Ajax.request中创建类似参数的东西 而不是success?或者也许有一种方法告诉form.submit()它必须检测其他responsece参数的成功?

BTW:a不能使用Ext.Ajax.request,因为我必须从表单传递多部分数据(文件上传).

提前致谢.

Jua*_*des 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');
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

如果你想使用不同的属性来表示错误,你应该查看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};
    }
});
Run Code Online (Sandbox Code Playgroud)

然后,当您创建表单时,您可以使用

 form : {
     errorReader: 'ns-customreader'
 }
Run Code Online (Sandbox Code Playgroud)

我还注意到你没有返回记录,读者应该根据文档来做,可能你只是不需要它,但是满足界面以保持代码在线是很好的使用Ext-JS

errorReader不一定是Reader的完整实现.它只需要实现一个read(xhr)函数,它返回一个具有以下结构的对象中的记录数组:

{ records: recordArray } // I think it needs success: boolean also.
Run Code Online (Sandbox Code Playgroud)