AJAX四步骤

AJAX由前端向后端发送并接收数据四步骤

<script type="text/javascript">
			window.onload = function()
			{
    
    
				document.getElementById("cal").onclick = function()
				{
    
    
					//1.创建异步对象
					var xmlHttp = new XMLHttpRequest();
					//2.绑定事件
					xmlHttp.onreadystatechange = function()
					{
    
    
						//处理服务器端返回的数据,更新当前页面
						//alert("readyState = " + xmlHttp.readyState + "|| status = " + xmlHttp.status);
						if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
						{
    
    
							var bmi = xmlHttp.responseText;
							document.getElementById("data").innerText = bmi;
						}
					}
					//3.初始化请求数据
					//获取用户输入的身高和体重值,并拼接为字符串填入参数中
					var weight = document.getElementById("w").value;
					var height = document.getElementById("h").value;
					//拼接为按照格式要求的字符串
					var param = "weight=" + weight + "&height=" + height;
					xmlHttp.open("GET","calbmi?" + param,true);
					//4.发起请求
					xmlHttp.send();
				}
			}
		</script>

猜你喜欢

转载自blog.csdn.net/weixin_46841376/article/details/112974288