鍍金池/ 問答/HTML/ 關(guān)于ajax和后端的一些小問題

關(guān)于ajax和后端的一些小問題

js部分:
window.onload = function() {


var oBtn = document.getElementById('btn');


oBtn.onclick = function() {
    
    var xhr = null;
    try {
        xhr = new XMLHttpRequest();
    } catch (e) {
        xhr = new ActiveXObject('Microsoft.XMLHTTP');
    }
    
    xhr.open('get','2.get.php?username=aaa&age=30&' + new Date().getTime(),true);

////問題1:在測試時(shí)給后端的數(shù)據(jù)是2.get.php?username=aaa&age=30&' + new Date().getTime();~~~&' + new Date().getTime()部分是和php里面不需要數(shù)據(jù),對php會有什么影響嗎?

    xhr.send();
    
    xhr.onreadystatechange = function() {
        
        if ( xhr.readyState == 4 ) {
            if ( xhr.status == 200 ) {
                alert( xhr.responseText );
            } else {
                alert('出錯(cuò)了,Err:' + xhr.status);
            }
        }
        
    }
    
}

}
</script>

后端文件:
<?php
header('content-type:text/html;charset="utf-8"');
error_reporting(0);

$username = $_GET['username'];
$age = $_GET['age'];

echo "你的名字:{$username},年齡:{$age}";

回答
編輯回答
陌如玉

不會有影響,只是為了不緩存ajax,傳給后端后端需要什么自己取相應(yīng)的字段就好了。相對而言,將獲取狀態(tài)變化觸發(fā)的函數(shù)放在send方法之前應(yīng)該會好一些哦。

2017年12月13日 04:09
編輯回答
久舊酒

后面加的時(shí)間戳,只是為了保證你每次的接口請求都是新的請求,而不是去讀的瀏覽器緩存信息

2018年2月2日 14:18
編輯回答
愛是癌

并沒有什么影響 避免請求被緩存而已

2017年8月1日 09:41
編輯回答
夢一場

我想知道是不是需要對傳過去的數(shù)據(jù)進(jìn)行encode編碼,在后端進(jìn)行uncode解碼

2017年6月2日 05:54