|
- <html>
- <head>
- <meta http-equiv="content-type" content="text/html;charset=utf-8" />
- <script type="text/javascript" src="https://libs.baidu.com/jquery/1.8.2/jquery.js"></script>
- <script type="text/javascript">
- $(function(){
- //使用json方式
- $("#send").click(function(){
- var cont = $("input").serialize();
- $("#result").html("提交中……");
-
- $.ajax({
- type : "POST",
- data:cont,
- async:false,
- url : "http://www.spw8.cn/e/user/api.php",
- dataType: "jsonp",
- jsonp: "callback",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(一般默认为:callback)
- jsonpCallback:"flightHandler",//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名,也可以写"?",jQuery会自动为你处理数据
- success: function(data){
- var str = "code:"+data.code + "<br>userid:"+data.username;
- $("#result").html(str);
- if(data.code==1){
- alert(data.username +"登陆成功");
- //使用示例
- setCookie("name",data.username);
- document.getElementById('delCookie').style.display="";
- }
- if(data.code==0){
- alert("账号密码错误");
- }
- },
- error: function(){
- alert("fail");
- },
- timeout: 3000
- });
- });
-
- //退出
- $("#delCookie").click(function(){
- var name=getCookie("name");
- alert(name+"退出成功");
- delCookie(name);
- });
-
- });
-
-
- </script>
- </head>
- <body>
- <div id="result">一会看显示结果</div>
- <form>
- <p><span>账号:</span><input type="text" name="username" value=""/></p>
- <p><span>密码:</span><input type="text" name="password" value=""/></p>
- </form>
- <button id="send">提交</button>
-
-
- <input type="button" id="delCookie" value="退出" style="display:none"/>
- </body>
- </html>
- <script>
- //写cookies
- function setCookie(name,value)
- {
- var Days = 30;
- var exp = new Date();
- exp.setTime(exp.getTime() + Days*24*60*60*1000);
- document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
- }
- //读取cookies
- function getCookie(name)
- {
- var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
- if(arr=document.cookie.match(reg)){
- return unescape(arr[2]);
- }else{
- return null;
- }
- }
- //删除cookies
- function delCookie(name)
- {
- var exp = new Date();
- exp.setTime(exp.getTime() - 1);
- setCookie("name","",-1);
- document.getElementById('delCookie').style.display="none";
- location.href="?";
- }
- (function(){
- var name=getCookie("name");
- if(name == "" || name == null ){
- alert("未登录");
- }else{
- alert(name+"已登录");
- document.getElementById('delCookie').style.display="";
- }
- })();
- </script>
复制代码 |
|