javascript ExtJS 4 Form.submit() 没有成功:回答为真
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16173474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ExtJS 4 Form.submit() without sucess:true in answer
提问by Andrew Kevich
I have a form, which submitted like this:
我有一个表单,提交如下:
form.submit({
url: '/postme',
waitMsg: 'Loading...',
method: 'POST',
success: function (form, action) {
console.log(action.response.responseText);
}
});
But! The main thing that in answer I don't have property success: true
and cannot manage to add it. Is there is a way to make something like callback
parameter in Ext.Ajax.requestinstead of success
? Or maybe there is a way to tell form.submit() that it must detect success in other responce parameter?
但!主要的问题是我没有财产success: true
并且无法添加它。有没有办法callback
在Ext.Ajax.request 中创建类似参数
而不是success
?或者也许有办法告诉 form.submit() 它必须在其他响应参数中检测成功?
BTW: a cannot use Ext.Ajax.request because I have to pass multipart data from form (file upload).
顺便说一句:a 不能使用 Ext.Ajax.request 因为我必须从表单(文件上传)传递多部分数据。
Thanks in advance.
提前致谢。
回答by Juan Mendes
The APIexplains that you have to pass success: true
for the success handler to be called. This is the doc for the submit
method
该API解释说,你要通过success: true
对被称为成功的处理程序。这是该submit
方法的文档
@param {Object} options The options to pass to the action (see Ext.form.Basic.submit and Ext.form.Basic.doAction for details)
@param {Object} options 传递给动作的选项(详见 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');
}
});
}
If you want to use a different property to indicate an error, you should look into http://docs.sencha.com/extjs/4.2.0/#!/api/Ext.form.Basic-cfg-errorReader, you can configure the errorReader to read your property as you would like.
如果您想使用不同的属性来指示错误,您应该查看http://docs.sencha.com/extjs/4.2.0/#!/api/Ext.form.Basic-cfg-errorReader,您可以配置 errorReader 以根据需要读取您的属性。
You can also route both your success and failure handlers to the same place and do determine it from the handler
您还可以将成功和失败处理程序路由到同一位置,并从处理程序中确定它
Be sure to set standardSubmit:true
so it doesn't get submitted with AJAX
一定要设置,standardSubmit:true
这样它就不会用 AJAX 提交
Sample solution that will make your code feel a little bit less dirty
示例解决方案将使您的代码感觉不那么脏
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};
}
});
Then, when you create the form, you can just use
然后,当您创建表单时,您可以使用
form : {
errorReader: 'ns-customreader'
}
I also noticed that you're not returning the record, which a reader should do according to the documentation, it may be that you just don't need it, but it'd be nice to satisfy the interface to keep your code in line with Ext-JS
我还注意到您没有返回记录,读者应该根据文档进行操作,可能只是您不需要它,但是满足接口以保持代码一致会很好使用 Ext-JS
The errorReader does not have to be a full-blown implementation of a Reader. It simply needs to implement a read(xhr) function which returns an Array of Records in an object with the following structure:
errorReader 不必是 Reader 的完整实现。它只需要实现一个 read(xhr) 函数,该函数在具有以下结构的对象中返回一个记录数组:
{ records: recordArray } // I think it needs success: boolean also.
回答by vahissan
If you don't send a success: true
it will obviously be a failure. So handle your parameter in the failure callback function similar to this, where your alternative to success
is status
:
如果你不发送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!');
}
}
});
回答by Ithar
In addition to @Juan Mendes answer I found the following conditions need to be true:
除了@Juan Mendes 的回答之外,我还发现以下条件必须为真:
On the server side
在服务器端
- The correct json needs to have a successtrue: {"success":true}
- The returned contentType must be set to text/html
- 正确的 json 需要成功true: {"success":true}
- 返回的 contentType 必须设置为text/html
This is true for version extjs-3.4.0
这适用于 extjs-3.4.0 版本
Please look at: Why success callback is not called in extjs form submission?
请看:为什么在extjs表单提交中没有调用success回调?
This saved me a lot of time and I hope it helps someone else.
这为我节省了很多时间,我希望它可以帮助其他人。