ExtJS을 이용한 RestFul한 통신하기
Scripter/EXTJS / 2008. 2. 26. 09:59
ExtJS에서 RESTful하게 HTTP status를 관리하게 생겼다. ㅠ.,ㅠ;
API 및 ExtJS의 코어을 뜯어본 결과 이넘들은 xhr.status >=200 && xhr.status <300 인경우만 처리하고 나머지는 모두 xhr.status 를 13030 으로 주고 있었다.
ext-base.js 에 보면 아래와 같이 처리 ㅠ.,ㅠ;
그리고 나서 모두 exception처리를 하고 있었는데
ScriptTagProxy의 경우 loadexception (
Ext.Ajax의 경우는 requestexception : (
기존의 예외처리를 위의 2개 이벤트를 사용해 처리해 줘야 하는데 아래와 같이 처리하면 되겠다.. ....
Ext.Ajax.on('beforerequest', function(conn, options) {
console.log(arguments);
} );
Ext.Ajax.on('requestcomplete', function(conn, response, options) {
console.log(arguments);
} );
Ext.Ajax.on('requestexception', function(conn, response, options) {
console.log(arguments);
} );
Ext.Ajax.request({
url: '/techbug.json?method=saveBranch',
success: function(response) {
branchList_store.load();
branchList.reconfigure(branchList_store, branchList_cm);
Ext.MessageBox.alert('Status', 'Your changes were saved successfully.');
},
failure: function(response) {
if(response.getResponseHeader.CustomHeader == "585")
Ext.MessageBox.alert('Status', 'You do not have permission to do this.');
else
Ext.MessageBox.alert('Status', 'There was an unknown error saving your changes.<P>Please try again.<P>Detailed Error: ' + response.getResponseHeader.CustomHeaderMessage);
},
headers: {
},
params: {
....
}
})
Ext.data.HttpProxy.on("loadexception", function(options, response, e) {
// do stuff for all load failures
});
Ext.data.HttpProxy.on("load", function(options, arg) {
// do stuff for all load successes
});
참고URL :
API 및 ExtJS의 코어을 뜯어본 결과 이넘들은 xhr.status >=200 && xhr.status <300 인경우만 처리하고 나머지는 모두 xhr.status 를 13030 으로 주고 있었다.
ext-base.js 에 보면 아래와 같이 처리 ㅠ.,ㅠ;
{
if (!J) {
this.releaseObject(I);
return
}
var G,
F;
try {
if (I.conn.status !== undefined && I.conn.status != 0) {
G = I.conn.status
} else {
G = 13030
}
} catch(H) {
G = 13030
}
if (G >= 200 && G < 300) {
F = this.createResponseObject(I, J.argument);
if (J.success) {
if (!J.scope) {
J.success(F)
} else {
J.success.apply(J.scope, [F])
}
}
} else {
switch (G) {
case 12002:
case 12029:
case 12030:
case 12031:
case 12152:
case 13030:
F = this.createExceptionObject(I.tId, J.argument, (E ? E: false));
if (J.failure) {
if (!J.scope) {
J.failure(F)
} else {
J.failure.apply(J.scope, [F])
}
}
break;
default:
F = this.createResponseObject(I, J.argument);
if (J.failure) {
if (!J.scope) {
J.failure(F)
} else {
J.failure.apply(J.scope, [F])
}
}
}
}
this.releaseObject(I);
F = null
}
if (!J) {
this.releaseObject(I);
return
}
var G,
F;
try {
if (I.conn.status !== undefined && I.conn.status != 0) {
G = I.conn.status
} else {
G = 13030
}
} catch(H) {
G = 13030
}
if (G >= 200 && G < 300) {
F = this.createResponseObject(I, J.argument);
if (J.success) {
if (!J.scope) {
J.success(F)
} else {
J.success.apply(J.scope, [F])
}
}
} else {
switch (G) {
case 12002:
case 12029:
case 12030:
case 12031:
case 12152:
case 13030:
F = this.createExceptionObject(I.tId, J.argument, (E ? E: false));
if (J.failure) {
if (!J.scope) {
J.failure(F)
} else {
J.failure.apply(J.scope, [F])
}
}
break;
default:
F = this.createResponseObject(I, J.argument);
if (J.failure) {
if (!J.scope) {
J.failure(F)
} else {
J.failure.apply(J.scope, [F])
}
}
}
}
this.releaseObject(I);
F = null
}
그리고 나서 모두 exception처리를 하고 있었는데
ScriptTagProxy의 경우 loadexception (
Object This
, Object o
, Object arg
, Object e
) 에서 argument형태로 처리되고 있으며Ext.Ajax의 경우는 requestexception : (
Connection conn
, Object response
, Object options
) 형태로 처리되고 있다.기존의 예외처리를 위의 2개 이벤트를 사용해 처리해 줘야 하는데 아래와 같이 처리하면 되겠다.. ....
Ext.Ajax.on('requestexception', function(c, r, o) {
debugger;
if (r.status == 402) {
//alert('Your session has expired');
}
else{
var error = r.responseText;
if (!error) error = r.statusText;
App.aSendError.createWindow(error);
}
})
debugger;
if (r.status == 402) {
//alert('Your session has expired');
}
else{
var error = r.responseText;
if (!error) error = r.statusText;
App.aSendError.createWindow(error);
}
})
Ext.Ajax.on('beforerequest', function(conn, options) {
console.log(arguments);
} );
Ext.Ajax.on('requestcomplete', function(conn, response, options) {
console.log(arguments);
} );
Ext.Ajax.on('requestexception', function(conn, response, options) {
console.log(arguments);
} );
Ext.Ajax.request({
url: '/techbug.json?method=saveBranch',
success: function(response) {
branchList_store.load();
branchList.reconfigure(branchList_store, branchList_cm);
Ext.MessageBox.alert('Status', 'Your changes were saved successfully.');
},
failure: function(response) {
if(response.getResponseHeader.CustomHeader == "585")
Ext.MessageBox.alert('Status', 'You do not have permission to do this.');
else
Ext.MessageBox.alert('Status', 'There was an unknown error saving your changes.<P>Please try again.<P>Detailed Error: ' + response.getResponseHeader.CustomHeaderMessage);
},
headers: {
},
params: {
....
}
})
Ext.data.HttpProxy.on("loadexception", function(options, response, e) {
// do stuff for all load failures
});
Ext.data.HttpProxy.on("load", function(options, arg) {
// do stuff for all load successes
});
Ext.Ajax.on('requestexception', function(c, r, o) {
debugger;
if (r.status == 402) {
//alert('Your session has expired');
}
else{
var error = r.responseText;
if (!error) error = r.statusText;
App.aSendError.createWindow(error);
}
})
debugger;
if (r.status == 402) {
//alert('Your session has expired');
}
else{
var error = r.responseText;
if (!error) error = r.statusText;
App.aSendError.createWindow(error);
}
})
// set up the connection to load the initial data
var loadConn = new Ext.data.Connection();
loadConn.request({method: 'POST', timeout: 120000, url: 'get_tasks_for_job.cfm' , params: { client_code: localClient, job: localJob, employee: localEmployee } });
// handle the response
loadConn.on('requestcomplete', function(sender, param) {
try
{
var response = Ext.util.JSON.decode(param.responseText);
} catch (e) {
alert('There was a problem loading the tasks for this job.');
return;
}
if (response.success == 'Y') {
// the init function will run loadData() as needed
localTaskData = response.data;
Ext.taskEditApp.main.init();
} else {
alert('There was a problem loading the tasks for this job.');
}
}, { scope: this });
loadConn.on('requestexception', function(conn, response, options) {
alert('failed!!!');
for (property in response) {
alert(property);
alert(response[property]);
}
}, { scope: this });
ds.load({params:{start:0, limit:pageCount},
callback:function(record, options, success){
if(!success){
alert('error occure.');
Ext.get('grid_id').unmask();
}
}
});
var loadConn = new Ext.data.Connection();
loadConn.request({method: 'POST', timeout: 120000, url: 'get_tasks_for_job.cfm' , params: { client_code: localClient, job: localJob, employee: localEmployee } });
// handle the response
loadConn.on('requestcomplete', function(sender, param) {
try
{
var response = Ext.util.JSON.decode(param.responseText);
} catch (e) {
alert('There was a problem loading the tasks for this job.');
return;
}
if (response.success == 'Y') {
// the init function will run loadData() as needed
localTaskData = response.data;
Ext.taskEditApp.main.init();
} else {
alert('There was a problem loading the tasks for this job.');
}
}, { scope: this });
loadConn.on('requestexception', function(conn, response, options) {
alert('failed!!!');
for (property in response) {
alert(property);
alert(response[property]);
}
}, { scope: this });
ds.load({params:{start:0, limit:pageCount},
callback:function(record, options, success){
if(!success){
alert('error occure.');
Ext.get('grid_id').unmask();
}
}
});
참고URL :
- http://extjs.com/forum/showthread.php?t=17879&highlight=requestexception
- http://extjs.com/forum/showthread.php?t=21311&highlight=requestexception
- http://extjs.com/forum/showthread.php?t=13185&highlight=status+transaction
- http://extjs.com/forum/showthread.php?t=21291&highlight=Ajax+Request+Success
- http://extjs.com/forum/showthread.php?t=5122&highlight=callback+argument+exception
'Scripter > EXTJS' 카테고리의 다른 글
Extjs 크로스 도메인 관련 (0) | 2008.02.28 |
---|---|
Extjs 한국어로 나타내기 (locale설정) (9) | 2008.02.27 |
GWT Ext 2.0 이 릴리즈됐다. (0) | 2008.02.26 |
ExtJS를 지원하는 IDE 개발이 시작되다. (0) | 2008.02.26 |
ExtTLD : XML을 이용한 Extjs 컴포넌트 생성 (0) | 2008.02.26 |