我需要使用Sencha Touch 2应用程序中某些服务器的API。为了使用此API,我需要在服务器上进行身份验证。
所以我已经实现了登录功能:
Ext.Ajax.request({
url: 'http://192.168.1.2:8080/spring-security-extjs-login/j_spring_security_check',
method: 'POST',
params: {
j_username: 'rod',
j_password: 'koala',
},
withCredentials: false,
useDefaultXhrHeader: false,
success: function(response){
var text = response.responseText;
Ext.Msg.alert("success", text, Ext.emptyFn);
},
failure: function(response){
var text = response.responseText;
Ext.Msg.alert('Error', text, Ext.emptyFn);
}
});
但是我如何调用API,因为在身份验证之后,我尝试调用API,但是他们已经想要身份验证。可能我需要保存JSESSIONID并将其添加到另一个请求中,但是我不知道该怎么做。
我不能使用withCredentials:true,所以我需要找到另一个解决方案。
如何从响应HTTP标头中获取Set-Cookies?
[我在Chrome控制台中看到,响应头中存在JSESSIONID,所以,我需要获取它。
[请帮助我找到任何解决方案。
您可以使用requestcomplete
和beforerequest
事件分别读取响应标头和写入请求标头。这是示例代码:
Ext.Ajax.on('requestcomplete', function(conn, response, options, eOpts){
var respObj = Ext.JSON.decode(response.responseText);
Helper.setSessionId(options.headers['JSESSIONID']);
}, this);
Ext.Ajax.on('beforerequest', function(conn, options, eOptions){
options.headers['JSESSIONID'] = Helper.getSessionId();
}, this);