我有一个extjs应用程序,该应用程序将ajax请求发送到后端。如果后端是活动会话,后端将发送json格式的对象,如果会话处于非活动状态,则将发送html页面
我想确定响应中是否接收到json或html类型,并相应地执行进一步的操作
这里是示例代码:
Ext.Ajax.Request({
url: "localhost",
scope: this,
method: "POST"
success: 'successongettingdata'
})
successongettingdata : function(connection,response) {
//check for response if html or json and do actions accordingly
//how to extract from response that if it is json or html or string
//if it is html, get form by its name
}
参考@incutonez,您可以从返回的请求中检查Content-Type
标头。
Ext.Ajax.request({
url: "localhost",
scope: this,
method: "POST",
success: 'successongettingdata'
});
successongettingdata : function(connection, response) {
if(connection.getResponseHeader("Content-Type") === "text/html") {
} else if(connection.getResponseHeader("Content-Type") === "application/json") {
}
}
或者如果Content-Type
错误,您可以尝试解码返回的数据。
successongettingdata : function(connection, response) {
try {
let decodeResponse = Ext.decode(connection.responseText);
//is json
} catch (e) {
//isn't json
}
}