python web框架企业实战详解(第六期)\第三课时-ajax&jquery&webpy

main.py

__author__ = 'Liao'

import web
import time

urls = (
    '/gettime','gettime',
    '/(.*)', 'hello'
)
app = web.application(urls, globals())

class gettime:
    def GET(self):
        asctime=time.asctime()
        print asctime
        return asctime
    def POST(self):
        return self.GET()

class hello:
    def GET(self, name):
        if not name:
            name = 'World'
        return 'Hello, ' + name + '!'

if __name__ == "__main__":
    app.run()

ajaxrawjs.html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<h1>this is a ajax rawjs page </h1>
<p>static files must be put in <font color="red" ><b> static</b></font> directory in webpy !</p>

<form name="myForm">
用户: <input type="text" name="username" οnkeyup="ajaxFunction();" />
时间: <input type="text" name="time" />
</form>


<script type="text/javascript">
function ajaxFunction()
 {
 var xmlHttp;

 try
    {
   // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
 catch (e)
    {

  // Internet Explorer
   try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
   catch (e)
      {

      try
         {
         xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
         }
      catch (e)
         {
         alert("您的浏览器不支持AJAX!");
         return false;
         }
      }
    }

    xmlHttp.onreadystatechange=function()
      {
      if(xmlHttp.readyState==4)
        {
         document.myForm.time.value=xmlHttp.responseText;
        }
      }
    xmlHttp.open("GET","/gettime",true);
    xmlHttp.send(null);

 }
</script>


</body>
</html>

ajaxjquery.html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>

    <!-- jquery下载地址:http://jquery.com/download/ -->
    <script src="jquery-1.12.1.min.js"></script>
</head>
<body>
<h1>this is a ajax jquery page </h1>
<p>static files must be put in <font color="red" ><b> static</b></font> directory in webpy !</p>

<form name="myForm">
用户: <input type="text" name="username" οnkeyup="ajaxFunction();" />
时间: <input type="text" id="time123" name="time" />
</form>

<script type="text/javascript">
function ajaxFunction()
 {
// alert("bbb");
// alert("bbb "+"cc");
// alert("bbb "+ $("input[name='username']").val() );
// $("input[name='time']").val(  $("input[name='username']").val()  );
// $("#time123").val(  $("input[name='username']").val()  );

  htmlobj=$.ajax({url:"/gettime",async:false,method:"GET"});
  $("#time123").val(htmlobj.responseText);
 }
</script>

</body>
</html>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值