PDA

View Full Version : Processing response in Ajax proxy



Elun
28 Apr 2012, 5:45 PM
Have an app: Data editor.
What i need to do: try to get(send) data from(to) server. Server checking: authorized user or not.
Authorized: good, proceed an operation.
If user not authorized, send a JSON anwser: {"success":false, "message":"401"} and after that response ExtJS app creating a form to LogOn. In stores I using Ajax proxy. How can i check a response from server?
Sorry for my bad english, that not my native language.

gnube
28 Apr 2012, 7:31 PM
You don't say how you are talking to the server, what information you are sending the server or how you are implementing authentication/sessions.

If you are using Ajax maybe like this?


Ext.Ajax.request({
url: serverHomeURL + '/YourPage.php',
waitMsg: 'Working...',

success: function(response){
//Do your thing
},
failure: function(response) {
//Do your thing
}
});

Elun
28 Apr 2012, 8:33 PM
Sorry, my bad.
I'm working with server like this:


Ext.define
('StoreName',
{
extend: 'Ext.data.Store',
model: 'ModelName',
autoLoad: true,
autoSync: true,
autoSave: true,
proxy:
{
type: 'ajax',
api:
{
read: 'URL',
update: 'URL',
create: 'URL',
destroy: 'URL'
},
reader:
{
type: 'json',
root: 'RootName',
successProperty: 'success'
},
writer: {
type: 'json',
encode: false,
listful: true,
writeAllFields: true,
getRecordData: function (record)
{
return { 'data': Ext.JSON.encode(record.data) };
}
},
headers: { 'Content-Type': 'application/json; charset=UTF-8' }
}
}
);
Operations with store:

var store = this.getStoreNameStore();
store.add(newElem);
store.remove(Elem);
record updating from form:

var win = button.up('window'),
form = win.down('form'),
record = form.getRecord(),
values = form.getValues();
record.set(values);
win.close();

gnube
28 Apr 2012, 8:48 PM
To my thinking, you will need to implement some 'session' handling on the server. This might be a 'login' URL to which you pass a username/password. Something like a cookie base PHP session which would allow the ajax server to check authentication and permissions on future requests.

This is not a trivial subject.

This forum is an good place to get specific answers to well defined questions but not really too good for looser ones. Most people just don't have that sort of time to spare :D

HTH

vietits
29 Apr 2012, 12:06 AM
With your request, I think the best place to handle is on 'requestcomplete' event of Ext.Ajax. Example:


Ext.Ajax.on('requestcomplete', function(connection, response){
try {
if (response.responseText) {
var result = Ext.JSON.decode(response.responseText);
if(result.success === false) {

switch(result.message) {
case '401':
// launch login form here;
break;
case '402':
Ext.Msg.show({
modal : true,
title : 'No Permission!',
msg : 'You do not have permission to access this function!',
icon : Ext.Msg.ERROR,
buttons: Ext.Msg.OK
});
break;
// handle other messages here
}
}
}
} catch(err) {
}
});

Elun
29 Apr 2012, 1:42 AM
vietits.
Thank you very much. Thats what that I need.