Ajax源代码
Ajax源代码
代码清单2-1,创建XMLHttpRequest对象
#001
#002
#013
代码清单2-2,将JavaScript嵌入html页面中
#001 …….
#002
#003
#004
#005 …..
#006 一般JavaScript写在这里
#007 ……
#008
#009
#010
#011
#012
#013 ……
代码清单2-3 标签应用示意
#001
#002
#006
代码清单2-4 创建XMLHttpRequest时处理IE版本差异#001
#002
#003 var xmlHttp = false;
#004 try {
#005 xmlHttp = new XMLHttpRequest();
#006 } catch (msfirstkind) {
#007 try {
#008 xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
#009 if(xmlHttp)
#010 alert("XMLHttpRequest对象由Msxml2.XMLHTTP创建");
#011 } catch (mssecondkind) {
#012 try {
#013 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
#014 if(xmlHttp)
#015 alert("XMLHttpRequest对象由Microsoft.XMLHTTP创建");
#016 } catch (failed) {
#017 xmlHttp = false;
#018 }
#019 }
#020 }
#021 if (!xmlHttp)
#022 alert("初始化XMLHttpRequest对象失败");
#023 //-->
#024
代码清单2-5 使用getAllResponseHeaders()得到HTTP响应全部信息
#001 var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.3.0");
#002 xmlhttp.open("GET", "http://localhost/sample.xml", false);
#003 xmlhttp.send();
#004 document.write(xmlhttp.getAllResponseHeaders());
代码清单2-6 GET方式提交的HTTP头内容:``
#001 GET /ajaxstudy/function.jsp? reqparam = reqparamvalue HTTP/1.1
#002 Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*
#003 Referer: http://localhost//example.html
#004 Accept-Language: zh-cn
#005 Accept-Encoding: gzip, deflate
#006 User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)
#007 Host: localhost:8080
#008 Connection: Keep-Alive
代码清单2-7 向服务器发送GET请求
#001
代码清单2-8 POST方式提交的HTTP头内容
#001 POST /ajaxstudy/function.jsp HTTP/1.1
#002 Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-
#003 powerpoint, application/vnd.ms-excel, application/msword, */*
#004 Referer: http://localhost//other.html
#005 Accept-Language: zh-cn
#006 Content-Type: application/x-www-form-urlencoded
#007 Accept-Encoding: gzip, deflate
#008 User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)
#009 Host: localhost:8080
#010 Content-Length: 9
#011 Connection: Keep-Alive
#012 reqparam=reqparamvalue
代码清单2-9 使用POST方式提交请求
#001
代码清单2-10 同时提交GET与POST请求
#001