今天 上网查了下Ext.ajax应用 看了 很有感觉贴下来以备以后之用
代码
Ext.Ajax.request({
url: 'Register.aspx',
params: {
oper: 'login',
LoginEmail:Ext.get('LoginEmail').dom.value,
LoginPwd:Ext.get('LoginPwd').dom.value
},
success: function(response, options) {
var responseArray = Ext.util.JSON.decode(response.responseText);
if(responseArray.success=='true'){
Cookies.set('Allcard_userName', responseArray.user);
Ext.Msg.alert('信息','您已成功登录!',IsLogin);
}
else{
Ext.Msg.alert('失败','登录失败,请确认您的帐号密码无误!');
}
}
});
url: 'Register.aspx',
params: {
oper: 'login',
LoginEmail:Ext.get('LoginEmail').dom.value,
LoginPwd:Ext.get('LoginPwd').dom.value
},
success: function(response, options) {
var responseArray = Ext.util.JSON.decode(response.responseText);
if(responseArray.success=='true'){
Cookies.set('Allcard_userName', responseArray.user);
Ext.Msg.alert('信息','您已成功登录!',IsLogin);
}
else{
Ext.Msg.alert('失败','登录失败,请确认您的帐号密码无误!');
}
}
});
代码说明:
2行:URL参数是要提交到的页面
3行:params是一个需要提交的参数集,使用逗号分隔
8行:当回调成功返回后要执行的函数
9行:获取服务器端的回调参数值
10行:对回调值进行判断处理
这里需要说明一下的是如何获取回调参数值,Extjs里是通过JSON的数据格式来获取参数的,因此在服务器端处理完事情后,回调的参数应该这样写:(以下是ASP.NET里的写法)
result = "{success:true,user:'winson'}";
Response.Write(result);
Response.End();
Response.Write(result);
Response.End();
以下再介绍下如何对JS对cookie 的操作
代码
var Cookies = {};
/**//**
* 设置Cookies
*/
Cookies.set = function(name, value){
var argv = arguments;
var argc = arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : '/';
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
};
/**//**
* 读取Cookies
*/
Cookies.get = function(name){
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
var j = 0;
while(i < clen){
j = i + alen;
if (document.cookie.substring(i, j) == arg)
return Cookies.getCookieVal(j);
i = document.cookie.indexOf(" ", i) + 1;
if(i == 0)
break;
}
return null;
};
/**//**
* 清除Cookies
*/
Cookies.clear = function(name) {
if(Cookies.get(name)){
var expdate = new Date();
expdate.setTime(expdate.getTime() - (86400 * 1000 * 1));
Cookies.set(name, "", expdate);
}
};
Cookies.getCookieVal = function(offset){
var endstr = document.cookie.indexOf(";", offset);
if(endstr == -1){
endstr = document.cookie.length;
}
return unescape(document.cookie.substring(offset, endstr));
};
/**//**
* 设置Cookies
*/
Cookies.set = function(name, value){
var argv = arguments;
var argc = arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : '/';
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
};
/**//**
* 读取Cookies
*/
Cookies.get = function(name){
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
var j = 0;
while(i < clen){
j = i + alen;
if (document.cookie.substring(i, j) == arg)
return Cookies.getCookieVal(j);
i = document.cookie.indexOf(" ", i) + 1;
if(i == 0)
break;
}
return null;
};
/**//**
* 清除Cookies
*/
Cookies.clear = function(name) {
if(Cookies.get(name)){
var expdate = new Date();
expdate.setTime(expdate.getTime() - (86400 * 1000 * 1));
Cookies.set(name, "", expdate);
}
};
Cookies.getCookieVal = function(offset){
var endstr = document.cookie.indexOf(";", offset);
if(endstr == -1){
endstr = document.cookie.length;
}
return unescape(document.cookie.substring(offset, endstr));
};