当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net mvc 2.0异步获取服务器时间

asp.net mvc 2.0异步获取服务器时间

2019年04月19日  | 移动技术网IT编程  | 我要评论

多进制相移键控,马赛丽,侠盗飞车4作弊器

  这一节给大家讲下mvc2.0中怎样使用ajax来异步获取信息,本节我们使用jquery中的ajax函数异步获取服务器端的时间。

  本节的主要内容

    1..net m 2.0中使用jquery的ajax函数

    2.服务器端时间和客户端时间通过json进行相互转换

 

首先,我们看下效果图,点击页面上的 服务器时间按钮,会从服务器端获取时间,并显示在页面上,此时客户端时间是不变的

 \


 

看下 view层的页面代码

[html]
<head runat="server"> 
    <title>用户列表页</title> 
    <script src="../../scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
        /** 
        * 时间对象的格式化; 
        */ 
        date.prototype.format = function (format) { 
            /* 
            * eg:format="yyyy-mm-dd hh:mm:ss"; 
            */ 
            var o = { 
                "m+": this.getmonth() + 1,  //month 
                "d+": this.getdate(),     //day 
                "h+": this.gethours(),    //hour 
                "m+": this.getminutes(),  //minute 
                "s+": this.getseconds(), //second 
                "q+": math.floor((this.getmonth() + 3) / 3),  //quarter 
                "s": this.getmilliseconds() //millisecond 
            } 
 
            if (/(y+)/.test(format)) { 
                format = format.replace(regexp.$1, (this.getfullyear() + "").substr(4 - regexp.$1.length)); 
            } 
 
            for (var k in o) { 
                if (new regexp("(" + k + ")").test(format)) { 
                    format = format.replace(regexp.$1, regexp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)); 
                } 
            } 
            return format; 
        } 
 
       
        window.onload = function () { 
 
            var testdate = new date(); 
 
            var teststr = testdate.format("yyyy-mm-dd hh:mm:ss"); 
 
            document.getelementbyid("clientdiv").innerhtml ="客户端时间:"+ teststr; 
 
        } 
 
        //异步获取服务器时间 
        function gettime()  
        { 
            $.ajax({ 
                type: "post", 
                url: "/user/gettime", 
                cache: false, 
                data: { id: "1" }, 
                success: function (output) { 
                    if (output == "" || output == undefined) { 
                        alert('返回值为空!'); 
                    } 
                    else { 
//                        value = new date(parseint(output.curtime.replace("/date(", "").replace(")/", ""), 10));  
                        value = new date(parseint(output.curtime.substr(6))); 
                        value = value.format("yyyy-mm-dd hh:mm:ss"); 
 
                        $('#pserver').html("服务器时间:" + value); 
                    } 
 
                }, 
                error: function (xmlhttprequest, textstatus, errorthrown) 
                 { 
                    alert("获取数据异常"); 
                } 
 
            }); 
         
        } 
    </script> 
</head> 
 
<body> 
  <p> 
 
  <input  type="button" value="服务器端时间" onclick="gettime();"/> 
        <p id="pserver"> 
        
        </p> 
        
 
        <p id="clientdiv"> 
         
        </p> 
        </p> 
</body> 

 

controller层

[csharp]
public jsonresult gettime() 
   { 
       if (request.isajaxrequest()) 
       { 
           //如果为ajax请求 
           var info = new models.info(); 
           info.id = "13"; 
           return this.json(info); 
       } 
       else 
       { 
           return null; 
       } 
       
   } 

 

 

model层

[csharp]
public class info 
    { 
        public string id 
        { 
            get; 
            set; 
        } 
 
        public datetime curtime 
        { 
            get { return datetime.now; } 
        } 
 
        
    } 

 

 

摘自 奶酪专栏

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网