I have an extjs application that sends an ajax request to the backend. The backend will send the objects which are json format if it is an active session and a html page if the session is inactive
I want to identify if it is a json or html type that is received in the response and perform further actions accordingly
Here is the sample code:
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
}
Referring to @incutonez, You can check Content-Type
header from returned request.
Ext.Ajax.request({
url: "localhost",
scope: this,
method: "POST",
success: 'successongettingdata'
});
successongettingdata : function(connection, response) {
if(connection.getResponseHeader("Content-Type").includes("text/html")) {
} else if(connection.getResponseHeader("Content-Type").includes("application/json")) {
}
}
Or you can try to decode returned data if Content-Type
is wrong.
successongettingdata : function(connection, response) {
try {
let decodeResponse = Ext.decode(connection.responseText);
//is json
} catch (e) {
//isn't json
}
}