Search code examples
ajaxextjsextjs4responsejavascript-framework

How to check if the response received from server is html or json and find the html form by name in extjs?


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
}

Solution

  • 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
       }
    }