上demo小案例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <input type="" name="" value="" id='nameTest'> <input type="button" value="发送ajax请求" id='btnAjax'> </body> </html> <script type="type/javascript"> document.querySelector("#btnAjax").onclick=function(){ //创建ajax var ajax=new XMLHttpRequest(); //设置ajax 的url //获取标签的值 var username=document.querySelector("#nameTest").value; ajax.open('get','ajax.php?name='+username); ajax.send(); ajax.onreadystatechange=function(){ if(ajax.readyState==4 && ajax.status==200){ console.log(ajax.responseText); } } } </script>
调用PHP文件路径
<?php echo $_GET['name'].'这道菜好吃' ?>