ExtJS 4 Form.submit () without sucess: true in the answer

I have a form that is presented as follows:

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

But! The main thing is that in response, I do not have the success: true property and cannot add it. Is there a way to do something like the callback parameter in Ext.Ajax.request instead of success ? Or maybe there is a way to tell form.submit () that it should detect success in another responce parameter?

BTW: a cannot use Ext.Ajax.request because I have to submit multipage data from a form (file upload).

Thanks in advance.

+4
source share
3 answers

The API explains that you must pass success: true for the calling success handler. This is the document for submit method.

Options

@param {Object} Parameters for transition to action (for more details see Ext.form.Basic.submit and 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 another property to indicate an error, you should examine http://docs.sencha.com/extjs/4.2.0/#!/api/Ext.form.Basic-cfg-errorReader , you can configure errorReader to read your property as you would like.

You can also route both your successful and error handlers to the same place and define it from the handler

Be sure to set standardSubmit:true so that it is not transmitted using AJAX

An example solution that will make your code a little 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 simply use

  form : { errorReader: 'ns-customreader' } 

I also noticed that you are not returning the record that the reader should do according to the documentation, maybe you just do not need it, but it would be nice to satisfy the interface so that the code is in accordance with Ext-JS

ErrorReader does not have to be a full-blown Reader implementation. You just need to implement a read function (xhr), which returns an array of records in an object with the following structure:

 { records: recordArray } // I think it needs success: boolean also. 
+9
source

If you do not send success: true , this will obviously be an error. So handle your parameter in a crash callback function like this, where your alternative to success is 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!'); } } }); 
0
source

In addition to @Juan Mendes answer, I found that the following conditions must be true:

Server side

  • The correct json must succeed true: {"success": true}
  • The returned contentType must be set to text / html

This is true for extjs-3.4.0

Take a look at Why is the success callback not invoked in the extjs form view?

It saved me a lot of time, and I hope it helps someone else.

0
source

All Articles