原生ajax异步请求问题--3次握手

本文通过一个简单的示例介绍了如何使用前端Ajax技术与后端Node.js进行数据交换。具体展示了前端如何发送HTTP请求及处理响应,并在后端设置服务器来响应这些请求。此外还解释了三次握手的过程及其对数据返回的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 前端代码
<script>
		 var xmlHttpReq = null;  
			//IE浏览器使用ActiveX  
			if (window.ActiveXObject) {  
				xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");  
			}  
			//其它浏览器使用window的子对象XMLHttpRequest  
			else if (window.XMLHttpRequest) {  
				xmlHttpReq = new XMLHttpRequest();  
			}
			var xmlHttpReq = new XMLHttpRequest();
			  xmlHttpReq.onreadystatechange = function () {
				if (xmlHttpReq.readyState == 4 && xmlHttpReq.status == 200) { 
					console.log(xmlHttpReq.responseText)   //console出"ccccc"
				}else {
					console.log("fail")            ////console出fail  3次
			    }
			  }
			 xmlHttpReq.open("get", "http://127.0.0.1:8888?cod=aa");
			 xmlHttpReq.send(); 
  </script>
 </body>





2 后端代码

var http=require('http');

var app=http.createServer(function(req,res){
	res.writeHead(200,{"content-type":"text/plain","Access-Control-Allow-Origin":"*"});
	res.end("ccccc")
})


app.listen(8888,function(){
	console.log("server is running")
})
结果是:前端先console出3个fail在console出"ccccc"
            应为前端要3次握手之后才有数据返回


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值