PDA

View Full Version : Ajax Request Error Handling



slsb02
22 Aug 2012, 12:07 AM
Hello,

what is the best / right way to handle response error?


... proxy: {
actionMethods: { create: 'POST', read: 'POST', update: 'POST', destroy: 'POST' },
extraParams: { task: "list" },
type: 'ajax',
url : File,
timeout: dataStoreLoad_Timeout,
reader: {
type: 'json',
root: 'results',
totalProperty: 'total',
},


An my test response is:

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version...

Firebug:

TypeError: result is undefined

if (result.success !== false) {

Thank you for Help!

scottmartin
22 Aug 2012, 10:38 AM
Do you mean something like the following?



<?php

define('ERROR_MESSAGE','THERE WAS AN ERROR!');

$return = array(
'success' => FALSE,
'message' => ERROR_MESSAGE
);

$return = json_encode($return);
echo $return

?>




Ext.onReady(function(){

Ext.create('Ext.Button', {
text: 'Click me',
renderTo: Ext.getBody(),
handler: function() {
Ext.Ajax.request({
url: 'myfunction.php',
success: function(xhr){
console.log('success');
var jResponse = Ext.decode(xhr.responseText);
if (jResponse.success === true) {
console.log('success::successful');
} else {
console.log('success::failed');
console.log(jResponse.message);
}
},
failure: function(error) {
console.log('failure'); // ajax error
},

callback: function(o, r, n){
console.log('callback');
}
});

}
});

});



Scott.

slsb02
22 Aug 2012, 8:28 PM
Hello,

No, I mean errors from Server.
I mean errors that are not predictable.

scottmartin
25 Aug 2012, 9:01 PM
You could create listeners as such:



Ext.Ajax.on('requestexception', function(conn, response, options) {
// console.log(response);
}, this);


You could also create an exception listener: on proxy/connection ...


listeners: {
exception: function(proxy, response, operation){
Ext.MessageBox.show({
title: 'REMOTE EXCEPTION',
msg: operation.getError(),
icon: Ext.MessageBox.ERROR,
buttons: Ext.Msg.OK
});
}
}


Scott