ajax POST方式資料傳遞

來源:互聯網
上載者:User

ajax POST方式資料傳遞
Ajax緩衝問題的解決:Ajax的本質就是將狀態儲存在用戶端,因此資源的緩衝和再利用是他的優勢所在,但有時候不希望被緩衝,例如計數器,不同請求的計數器得到的結果應該是最新的。線上時間長度也應該每次重新整理不一樣。     1)設定隨機數:   Math.random();url: "user.php?username="+username+"&num="+Math.random();      2)設定時間戳記var dateTime = new Date().getTime()    url: "user.php?username="+username+"&num="+dateTime;     3)使用POST代替GET方式提交資料: POST本身提交和返回的資料的不緩衝;       4)設定回應標頭資訊       header("Cache-Control:no-cache"); POST方式資料傳遞:    1、在send前 設定post發送的資料按照URL地址方式傳遞        ajax.setRequestHeader("content-Type","application/x-www-form-urlencoded");    2、open函數中第二個參數只剩下提交地址    3、將傳遞的資料: 參數名=值&參數名=值&...的方式放入到send函數()中ajax.open("POST","./user.php",false);//發送ajax.setRequestHeader("content-TYPE","application/x-www-form-urlencoded");var data = "uname="+uname;ajax.send(data); reg.html 

<!DOCTYPE HTML><html>    <head>        <title> ajax </title>        <meta charset="utf-8"/>        <script type="text/javascript">            var httpAjax = new XMLHttpRequest();            function checkUser(uname){                if(uname == ""){                    return false;                }                httpAjax.onreadystatechange = function(){                    if(httpAjax.readyState == 4 && httpAjax.status == 200){                        var res = httpAjax.responseText;                        var sp = document.getElementById("sp");                        if(res == "true"){                            sp.innerHTML = "<font color='red'>登入</font>";                        }else{                            sp.innerHTML = "<font color='green'>可以註冊</font>"                        }                    }                }                httpAjax.open("post","user.php",true);                httpAjax.setRequestHeader("content-TYPE","application/x-www-form-urlencoded");

 

                var data = "uname="+uname; //user.php?uname="1"&pwd="123","uname=" 是參數,+是參數串連變數的用的,uname是js中的一個不帶$的變數,也就是值                httpAjax.send(data);            }        </script>    </head>    <body>        <input type="text" id="username" class="username" name="username" onchange="checkUser(this.value)"/><span id="sp"></span>    </body></html>user.php 
<?php    header("content-type:text/html;charset=utf-8");    $pdo = new PDO("mysql:host=localhost;dbname=tk106","root","");    $pdo->exec("set names utf8");    $uname = $_REQUEST["uname"];//post傳值,這裡要改為REQUEST接收    $sql = "select * from stu_info where sname='".$uname."'";    $data = $pdo->query($sql)->fetch(PDO::FETCH_ASSOC);    if($data){        echo "true";    }else{        echo "false";    }?>

 

<?php    header("content-type:text/html;charset=utf-8");    $pdo = new PDO("mysql:host=localhost;dbname=tk106","root","");    $pdo->exec("set names utf8");    $uname = $_REQUEST["uname"];//post傳值,這裡要改為REQUEST接收    $sql = "select * from stu_info where sname='".$uname."'";    $data = $pdo->query($sql)->fetch(PDO::FETCH_ASSOC);    if($data){        echo "true";    }else{        echo "false";    }?>

 


聯繫我們

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