是的,有,但不,你不应该使用它。
在您的情况下,在 ExtJS 6 之前的 ExtJS 中,您可以轻松地从 getStore().getProxy().getReader().rawData 读取整个原始响应,除非您在阅读器定义中设置了 keepRawData:false。这带有一个很大的但是。
在 6.0 中,如果您不在阅读器上使用 keepRawData,那么原始响应数据会很早就被丢弃。原因,引用自文档:请注意,从 Ext JS 6.0 开始,默认行为已更改为不保留原始数据,因为内存泄漏的可能性很高。
因此,我在我的阅读器上为 ExtJS 6.0.1 添加了一个覆盖:
Ext.define("MyApp.override.JsonReader", {
override:"Ext.data.reader.Json",
/**
* Make additional processing available on the raw response.
*/
processRawResponse:null,
getResponseData:function(response) {
if(this.processRawResponse) this.processRawResponse(response);
me.callParent(arguments);
}
});
所以现在我可以根据需要在每个阅读器上添加一个 processRawResponse 函数,比如这个:
proxy: {
type: 'ajax',
groupParam: false,
startParam:false,
limitParam:false,
pageParam:false,
sortParam:false,
url: '../api/AdminPanel/ACLRules',
headers: {
Accept: 'application/json'
},
reader: {
type: 'json',
rootProperty: 'data',
processRawResponse:function(response) {
var responseText = Ext.decode(response.responseText, true);
if(responseText && responseText.message) {
Ext.Msg.alert('ERROR',responseText.message);
}
你可以试试这是否也适用于 ExtJS 4; from the source code, it seems so。否则,只需要进行微小的更改。
如果您的错误消息每次都出现在同一个属性中并且每次都需要相同的处理,那么您也可以直接从覆盖全局处理它们,例如如果你得到一个调试信息数组:
getResponseData:function(response) {
if(this.processRawResponse) this.processRawResponse(response);
var debugList = Ext.getCmp("debugList"),
shorten = function(tex) {
return tex.substring(0,500).replace(/(\r\n|\r|\n)/g,"<br>");
},
returned = "";
try {
returned = response.responseText;
var decodedData = Ext.decode(returned);
if(decodedData.Debug) this.rawDebugData = decodedData.Debug;
return decodedData;
} catch (ex) {
var caption = "Decoding error",
message = "The server has returned malformed JSON.",
box = Ext.create('Ext.window.MessageBox',{});
try {
var jsonStart = Math.min(returned.indexOf('['), returned.indexOf('{'));
if(jsonStart>0) {
message = (message + "\n" + returned.substring(0,jsonStart));
returned = returned.substring(jsonStart);
}
else {
message = (message + "\n" + returned);
}
} catch (e) {
}
if(!debugList) box.alert(caption,shorten(message));
if(debugList) debugList.setValue(debugList.getValue()+caption+': '+message+'\n');
return Ext.decode(returned, true) || {};
}
},