javascriptとサーバ2
10895 ワード
, IE XMLHTTP
XMLHTTP , MSXML XMLHTTP , XMLHTTP XML http . IE XMLHTTP , ActiveXObject .
1.
var oRequest = new ActiveXObject("Microsoft.XMLHTTP");
,XMLHTTP , XMLHTTP , :
function createXMLHTTP(){
var arr =["MSXML2.XMLHTTP5.0", "MSXML2.XMLHTTP4.0",
"MSXML2.XMLHTTP3.0", "MSXML2.XMLHTTP",
"Microsoft.XMLHTTP" ]
for(var i=0; i<arr.length; i++){
try{
var oRequest = new ActiveXObject(arr); // XMLHTTP
return oRequest;
}
catch(oError){
//
}
}
throw new Error("MSXML is not installed on your system."); // MSXML,
}
var oRequest =createXMLHTTP(); // XMLHTTP , ,
2. open , URL,
oRequest.open("get","example.txt",false) // , 1. 2.URL 3. ,false = , true=
3. , , onreadystatechange
oRequest.send(null);
send , , get null, URL , post :name1=value1&name2=value2 .
, ,javascript , ,status HTTP (200 ,404 ). ,statusText , responseText . xml, reponseXML
XML DOM .
:
var oRequest =createXMLHTTP();
oRequest.open("get","example.txt",false) //
oRequest.send(null); // get
alert("Status is "+ oRequest.status +"( "+ oRequest.statusTest +")"); // ,
alert("Response text is :"+ oRequest.responseText); //
xml, responseXML .
oRequest.responseXML.documentElement.tagname; // xml
, xml http mime xml .
, , onreadystatechange , readyState 4 . reponse . , :
var oRequest =createXMLHTTP();
oRequest.open("get","example.txt",true) //
oRequest.onreadystatechange = function(){
if(oRequest.readyState ==4)
{
alert("Status is "+ oRequest.status +"( "+ oRequest.statusTest +")"); // ,
alert("Response text is :"+ oRequest.responseText); //
}
}
oRequest.send(null); // get
, , onreadystatechange , readystate .
abort() , :
var oRequest =createXMLHTTP();
oRequest.open("get","example.txt",true) //
oRequest.onreadystatechange = function(){
if(oRequest.readyState ==3){
oRequest.abort(); // , .
}
else if(oRequest.readyState ==4)
{
alert("Status is "+ oRequest.status +"( "+ oRequest.statusTest +")"); // ,
alert("Response text is :"+ oRequest.responseText); //
}
}
oRequest.send(null);
4. http
HTTP HTTP , , . HTTP , , , client server .
XMLHTTP , HTTP .
getAllResponseHeaders() HTTP , :
Date: Sun,14 Nov 2009 20:10:00 GMT
Server: Apache/1.3 (Unix)
Vary:Accept
X-Powered-By:PHP/4.3.8
Connection:close
Content-Type: text/html;charset=iso-8859-1;
, , , php . , :
var sDate = oRequest.getResponseHeader("Date"); //
Http
oRequest.setRequestHeader("name","XX");
oRequest.setRequestHeader("age",18);
. Mozilla XMLHTTP
IE XMLHTTP web , , Mozilla , XMLHttpRequest . IE , , Safari Opera Mozilla , XMLHttpRequest XMLHTTP .
Mozilla XMLHTTP :
var oRequest = new XMLHttpRequest();
. XMLHTTP
XMLHttpRequest . .
function createXMLHttpRequest(){
if (typeof XMLHttpRequest == "undefined" && window.ActiveXObject) { // , , XMLHttpRequest XMLHTTP .
function XMLHttpRequest() {
var arrSignatures = ["MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0",
"MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP",
"Microsoft.XMLHTTP"];
for (var i=0; i < arrSignatures.length; i++) {
try {
var oRequest = new ActiveXObject(arrSignatures);
return oRequest;
} catch (oError) {
//ignore
}
}
throw new Error("MSXML is not installed on your system.");
}
}
else
return new XMLHttpRequest();
}
}
var xmlHttp=createXMLHttpRequest(); // XMLHTTP
XMLHTTP , . .