2016年3月17日学习笔记----AJAX(网络协议)

function btnClick() {
      var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");//创建XMLHTTP对象,相当于WebClient
if(!xmlhttp){
alert("创建xmlhttp对象异常!");
return false;
}
xmlhttp.open("POST","GetDatel.ashx?id="+encodeURI("中国")+"&ts"+new Date(),false);// 准备向服务器的 GetDate1.ashx发出POST请求。
//XMLHTTP默认的(也推荐)不是同步请求的,也就是open方法不像WebClient的DownLoadString那样把服务器的数据拿到才返回,是异步的,因此需要监听onreadystatechange事件。
xmlhttp.onreadystatechange = function() {
 if (xmlhttp.readyState == 4) {
    if(xmlhttp.status == 200){//如果状态码为200则是成功
        document.getElementById("TExt1").value =xmlhttp.responseText;//responseText属性为服务器返回的文本
else {
alert("AJAX服务器返回错误!");  
    }
  }
}
xmlhttp.send()://这时才开始发送请求
}

 含有中文要使用encodeURI进行编译

汇率计算代码:

原文地址:https://www.cnblogs.com/quwujin/p/5286608.html