PDA

View Full Version : Form.load success false root undefined



senacle
27 Jan 2011, 6:34 AM
Hi !

I'm loading data to a form.
When my php script returns data in json format with success:true, all is fine.
But if my php script return a success:false like this :


{success:false, errors:'No data found'}
there is this error :



root is undefined
readRecords()
o = Object {success=false, errors="No data found"} ext-all-debug.js (ligne 25347)


Here'is my form.load :



form.load({
failure: function(form, action) {
Ext.Msg.alert("Error.", action.result.errors);
},
params: {...},
url: 'url.php',
success: function(form, action) {
...
},
waitMsg: 'Loading...'
});
I don't see what's wrong.

arthurakay
27 Jan 2011, 7:20 AM
Are you sure that your script is hitting the "failure" callback?

One of the most common mistakes people make is that the "success" callback fires instead of the "failure" callback - it ONLY fires if the server returns a failure response in the header (like 404, 500, etc).

Use Firebug to set a breakpoint in your "success" callback. See if you are hitting it when the your submission fails. If so, the problem has 2 components:
1) you're hitting the wrong handler on failure...
2) ...which causes a syntax error in your "success" callback

senacle
27 Jan 2011, 11:00 AM
The sample provided in the API could be false ? Look at : http://dev.sencha.com/deploy/dev/docs/output/Ext.form.Action.Load.html

I'm doing the same...

So i don't understand.

arthurakay
27 Jan 2011, 11:05 AM
The example isn't wrong... incomplete perhaps, but not wrong.

The "failure" method won't be called unless the response header for the submission contains a failure response code.

In other words, your server could return a value of...

{
success: false,
errorMessage: "Consignment reference not found"
}

...but the failure method WILL NOT BE CALLED unless the response header contains a 404/500/etc code.

In PHP:

header("HTTP", true, 500);

If your response header doesn't have that failure code (it defaults to 200 for "success") your form will run the "success" handler.

senacle
28 Jan 2011, 1:10 AM
This behaviour is only for the failure function for a form load ?

Because i'm using in different places the failure function (in form.submit) and it's OK.

senacle
28 Jan 2011, 1:44 AM
OK, it works as you said, but i'm very surprised of this behaviour, because in all other cases, the failure function doesn't work like this.

My code :


form.load({
failure: function(form, action) {
Ext.Msg.alert("Error.", action.response.responseText);
},
params: {...},
url: 'url.php',
success: function(form, action) {
...
},
waitMsg: 'Loading...'
});

On the php side (no need to specify success: false) :



header("HTTP", true, 500);
echo utf8_encode($msgerror);

senacle
28 Jan 2011, 2:09 AM
OK, it works as you said, but i'm very surprised of this behaviour, because in all other cases, the failure function doesn't work like this.

My code :



form.load({
failure: function(form, action) {
Ext.Msg.alert("Error.", action.response.responseText);
},
params: {...},
url: 'url.php',
success: function(form, action) {
...
},
waitMsg: 'Loading...'
});On the php side (no need to specify success: false) :



header("HTTP", true, 500);
echo utf8_encode($msgerror);

senacle
28 Jan 2011, 6:28 AM
OK, it works as you said, but i'm very surprised of this behaviour, because in all other cases, the failure function doesn't work like this.

My code :



form.load({
failure: function(form, action) {
Ext.Msg.alert("Error.", action.response.responseText);
},
params: {...},
url: 'url.php',
success: function(form, action) {
...
},
waitMsg: 'Loading...'
});On the php side (no need to specify success: false) :



header("HTTP", true, 500);
echo utf8_encode($msgerror);

arthurakay
30 Jan 2011, 8:42 AM
Any of the methods which piggy-back on the Ext.Ajax methods (like Ext.Ajax.request() ) work the same way. From the API docs:


failure: Function
The callback that will be invoked after a failed transaction attempt.

In other words, if an error is encountered (like a 500 response) Ext will use the failure callback. If no error is encountered, it uses the success callback.

Looking at the API docs again, their example is:


{
success: false,
errors: {
clientCode: "Client not found",
portOfLoading: "This field must not be null"
}
}


The label for that example says: "By default, response packets are assumed to be JSON, so a typical response packet may look like this" - but in reality the "success" property doesn't do anything. ExtJS doesn't look for it. It's just an example of how to structure a JSON response.

If things are "working" in other areas of your code, I don't know what to tell you without seeing specific examples. Perhaps they are working but you incorrectly understand WHY they're working.

valititi
30 Jan 2011, 11:43 AM
hey senacle !
failure NEVER means success:false !!!
the failure is only related to failure communication with the server !
success control THE ANSWER FROM A SUCCESSFUL connection !
Your json answer, if it's return success:true, or success:false, both must be managed on the success event !


success: function(form, action) {
if(success){
// here controls for success:true
}
else{
// here controls for your json success:false
}
},
// failure: function only for failure communications with server...

hope this can help !

senacle
31 Jan 2011, 12:33 AM
First, sorry for posting the same contrib (above) 3 times...


So, you explain that the failure is only related to failure communication with the server.

Hum !

Can you explain me this behaviour :


form_abonnement.submit({
failure: function(result, action) {
obj = Ext.decode(action.response.responseText);
var message = obj.errors;
Ext.Msg.show({
title: 'Information',
msg: message,
closable: false,
buttons: Ext.MessageBox.OK,
icon: Ext.MessageBox.INFO
});
},
params: {abonnement: abonnement},
success: function (result, action) {
obj = Ext.decode(action.response.responseText);
var message = obj.message;
Ext.Msg.show({
buttons: Ext.MessageBox.OK,
closable: false,
fn: function(buttonId, text, opt){
if (obj.origine == 'administration') {
fenetre_abonnement.hide();
grid_utilisateur.store.reload();
}
},
icon: Ext.MessageBox.INFO,
msg: message,
title: 'Information'
});
},
waitMsg: 'Enregistrement en cours...'
});
I submit a form.
The php script send a json response like that :

if ($msgerror != "") {
echo "{success:false, errors:'".addslashes($msgerror)."'}";
} else {
echo "{success:true, message:'".addslashes($message_ok)."', origine:'".$origine."'}";
}
I simulate an error on the php side, but not a server error, so the json is
echo "{success:false, errors:'error message test'}";And on the client side, i come in failure function !

In the action object, there's :

failureType : "server"
response : responseText : "{success:false, errors:'error message test'}"
status : 200
statusText :"OK"
result : errors : "error message test"
success : falseWhen i simulate no error, the json is
echo "{success:true, message:'true message test'}";In the action object, there's :

response : responseText : "{success:true, errors:'true message test'}"
status : 200
statusText :"OK"
result : message : "true message test"
success : trueAfter looking at the form Action API :

LOAD_FAILURE (http://dev.sencha.com/deploy/dev/docs/source/Action.html#prop-Ext.form.Action-LOAD_FAILURE) : String
Failure type returned when the response's success property is set to false, or no field values are returned in the response's data property.

SERVER_INVALID (http://dev.sencha.com/deploy/dev/docs/source/Action.html#prop-Ext.form.Action-SERVER_INVALID) : StringFailure type returned when server side processing fails and the result (http://dev.sencha.com/deploy/dev/docs/output/Ext.form.Action.html#Ext.form.Action-result)'s success property is set to false. In the case of a form submission, field-specific error messages may be returned in the result (http://dev.sencha.com/deploy/dev/docs/output/Ext.form.Action.html#Ext.form.Action-result)'s errors property.
So, valititi and arthurakay, you're right in your explanations, but for the 2 cases above, the failure function is also called when the success is set to false...

arthurakay
31 Jan 2011, 6:34 AM
@senacle

After re-reading your original post and your latest response, I need to backtrack for a moment.

My last response said something to to effect that the "success" property was simply an example of a JSON response. I was incorrect and I apologize for the confusion.

Ext.form.Action does use the "success" property of the response JSON - but that isn't the entire story.

The AJAX "success" and "failure" callbacks are determined in the following way:

(1) for "failure", either the connection to the server failed (like a 500 error) or the "failureType" property was detected (more on that in a minute)

(2) for "success", the connection to the server succeeded AND no "failureType" is detected

Thanks for calling me out... I completely misread the API docs on that one. :">

So after all of that... is your problem solved?

senacle
31 Jan 2011, 11:36 PM
I apologize for the confusion.

Don't worry ;)
After your first explanation, i need also to read again the API. Thus, i understand new things about failure function, if it's used in a submit or a load or something else...



So after all of that... is your problem solved?


Sure !:)

senacle
29 Feb 2012, 2:19 AM
I come back to the original post.

I understand now that for a form load, the reader of the form is waiting for some data, regardless of whether success is true or false.

So, when the server needs to send a success: false, the json must contain the root of the reader (set it to empty value) :



{success:false, root_of_the_form_s_reader: '', errors:'No data found'}