jq和thinkphp經常使用的幾種ajax

來源:互聯網
上載者:User

標籤:style   blog   http   color   java   使用   io   資料   

第一種方法


 

 

 

第二種方法

jquery方法:MessageAction.class.php<?phpclass MessageAction extends Action{       function index(){        $this->display();       }       function add(){        //ajaxReturn(資料,‘提示資訊‘,狀態)           $m=M(‘message‘);        if($m->add($_GET)){            $this->ajaxReturn($_GET,‘添加資訊成功‘,1);        }else{            $this->ajaxReturn(0,‘添加資訊失敗‘,0);           }    } }?>模板index.html<html><head><script type="text/javascript" src="__PUBLIC__/js/jquery-1.7.1.min.js"></script><script type="text/javascript">    $(function(){        $(‘input:button‘).click(function(){            var $title=$(‘input[name="title"]‘).val();            var $message=$(‘input[name="message"]‘).val();            $mess=$(‘#mess‘);           $.getJSON(‘__URL__/add‘,{title:$title,message:$message},function(json){                //alert(json);return false;                if(json.status==1){                    $mess.slideDown(3000,function(){                        $mess.css(‘display‘,‘block‘);                       }).html(‘標題為‘+json.data.title+‘資訊為‘+json.data.message);                   }else{                    $mess.slideDown(3000,function(){                        $mess.css(‘display‘,‘block‘);                       }).html(‘資訊添加失敗,請檢查‘);                   }                   });        })       })</script></head><body><div style="display:none; color:red;" id="mess"></div><form action="" method="get">  標題:<input type="text" name="title" /><br />  資訊:<input type="text" name="message" /><br />       <input type="button" value="提交" /></form></body></html>ThinkPHP方法:MessageAction.class.php<?phpclass MessageAction extends Action{       function index(){        $this->display();       }    function addtwo(){        $m=M(‘message‘);        if($vo=$m->create()){            if($m->add()){                $this->ajaxReturn($vo,‘添加成功‘,1);               }else{                $this->ajaxReturn(0,‘添加失敗‘,0);               }           }else{            $this->error($m->getError());           }                  } }?>模板index.html<html><head><script type="text/javascript" src="__PUBLIC__/Js/Base.js"></script><script type="text/javascript" src="__PUBLIC__/Js/prototype.js"></script><script type="text/javascript" src="__PUBLIC__/Js/mootools.js"></script><script type="text/javascript" src="__PUBLIC__/Js/ThinkAjax.js"></script><script type="text/javascript">    function add(){        //ThinkAjax.sendForm(表單ID,URL,回呼函數,資訊顯示的地方);        ThinkAjax.sendForm(‘frm‘,‘__URL__/addtwo‘,wc);       }    function wc(data,status){        if(status!=1){            alert(‘發送失敗‘);        }else{            $(‘list‘).innerHTML+=‘標題‘+data.title+‘,資訊‘+data.message;           }       }</script></head><body>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.