jQuery簡單的Ajax調用樣本

來源:互聯網
上載者:User

標籤:

jQuery確實方便,下面做個簡單的Ajax調用:

建立一個簡單的html檔案:

<!DOCTYPE HTML><html><head><script type="text/javascript" src="Public/js/jquery-easyui-1.3.1/jquery-1.8.2.min.js"></script><script type="text/javascript">    $(function(){        //按鈕單擊時執行        $("#testAjax").click(function(){              //取Ajax返回結果              //為了簡單,這裡簡單地從檔案中讀取內容作為返回資料              htmlobj=$.ajax({url:"/Readme.txt",async:false});               //顯示Ajax返回結果               $("#myDiv").html(htmlobj.responseText);         });    });</script>    </head>    <body>        <div id="myDiv"><h2>通過 AJAX 改變文本</h2></div>        <button id="testAjax" type="button">Ajax改變內容</button>    </body></html>

好了,點擊按鈕就可以看到效果了。

當然,jQuery的Ajax調用可以控制項很多,這裡示範了簡單的調用。

注意你自己的jquery引用路徑。

 

好吧,做一個調用背景例子:

<!DOCTYPE HTML><html><head><script type="text/javascript" src="Public/js/jquery-easyui-1.3.1/jquery-1.8.2.min.js"></script><script type="text/javascript">    $(function(){        //按鈕單擊時執行        $("#testAjax").click(function(){                            //Ajax調用處理            var html = $.ajax({               type: "POST",               url: "test.php",               data: "name=garfield&age=18",               async: false            }).responseText;            $("#myDiv").html(‘<h2>‘+html+‘</h2>‘);         });    });</script>    </head>    <body>        <div id="myDiv"><h2>通過 AJAX 改變文本</h2></div>        <button id="testAjax" type="button">Ajax改變內容</button>    </body></html>

後台代碼:

<?php    $msg=‘Hello,‘.$_POST[‘name‘].‘,your age is ‘.$_POST[‘age‘].‘!‘;    echo $msg;

現在已經可以從後台來擷取資料了!

 

當然,我們還可以這樣來調用Ajax:

<!DOCTYPE HTML><html><head><script type="text/javascript" src="Public/js/jquery-easyui-1.3.1/jquery-1.8.2.min.js"></script><script type="text/javascript">    $(function(){        //按鈕單擊時執行        $("#testAjax").click(function(){                            //Ajax調用處理            $.ajax({               type: "POST",               url: "test.php",               data: "name=garfield&age=18",               success: function(data){                        $("#myDiv").html(‘<h2>‘+data+‘</h2>‘);                  }            });                     });    });</script>    </head>    <body>        <div id="myDiv"><h2>通過 AJAX 改變文本</h2></div>        <button id="testAjax" type="button">Ajax改變內容</button>    </body></html>

 

注意,

success: function(data)

中的data參數可以改為別的名稱,比如success: function(msg),msg(data)為返回的資料。 此處為回呼函數的參數,而非

data: "name=garfield&age=18"

中的Ajax調用中的data參數。


jQuery簡單的Ajax調用樣本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.