如何检查从服务器收到的响应是html还是json,并在extjs中按名称查找html表单?

问题描述 投票:0回答:1

我有一个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
}
ajax extjs extjs4 response javascript-framework
1个回答
0
投票

参考@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
   }
}
© www.soinside.com 2019 - 2024. All rights reserved.