1 前端代码
应为前端要3次握手之后才有数据返回
<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次握手之后才有数据返回