如何检查从服务器收到的响应是 html 还是 json 并在 extjs 中按名称查找 html 表单?
How to check if the response received from server is html or json and find the html form by name in extjs?
我有一个向后端发送 ajax 请求的 extjs 应用程序。如果是活动会话,后端将发送 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
header。
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")) {
}
}
或者如果 Content-Type
错误,您可以尝试解码返回的数据。
successongettingdata : function(connection, response) {
try {
let decodeResponse = Ext.decode(connection.responseText);
//is json
} catch (e) {
//isn't json
}
}
我有一个向后端发送 ajax 请求的 extjs 应用程序。如果是活动会话,后端将发送 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
header。
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")) {
}
}
或者如果 Content-Type
错误,您可以尝试解码返回的数据。
successongettingdata : function(connection, response) {
try {
let decodeResponse = Ext.decode(connection.responseText);
//is json
} catch (e) {
//isn't json
}
}