Как проверить, получен ли от сервера ответ html или json, и найти форму html по имени в ext js? - PullRequest
0 голосов
/ 30 апреля 2020

У меня есть приложение ext js, которое отправляет запрос 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
}

1 Ответ

1 голос
/ 30 апреля 2020

Обращаясь к @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
   }
}
...