標籤:
ajax資料請求5(php格式):
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>ajax請求資料3</title></head><body><button id="btn">請求資料</button><h1 id="txt"></h1><script type="text/javascript">var btn = document.getElementById(‘btn‘);var txt = document.getElementById(‘txt‘);btn.onclick = function() {//1.建立XMLHttpRequest對象var xhr = null;if(window.XMLHttpRequest) {//非IE56xhr = new XMLHttpRequest();//執行個體對象}else {//IE56xhr = new ActiveXObject(‘Microsoft.XMLHTTP‘);}//2.開啟與伺服器的連結xhr.open(‘get‘,‘test3.php?_=‘ + new Date().getDate(),true);//產生不一樣的url解決緩衝問題//3.發送給伺服器xhr.send(null);//get請求//4.響應就緒xhr.onreadystatechange = function() {if (xhr.readyState == 4) {//請求完成if(xhr.status == 200) {var json = JSON.parse(xhr.responseText);txt.innerHTML = json.sex;}else {alert(xhr.status);}}}}</script></body></html> PHP格式:
<?php$str = ‘{"name":"老王","sex":"男","age":"22","score":"58"}‘;echo $str;?>
ajax資料請求5(php格式)