http-request.js

来自「这是《JavaScript学习源代码》一书的源代码」· JavaScript 代码 · 共 35 行

JS
35
字号
/* 

This example must be run from a web server... for example by uploading to your ISP and viewed over the web, or placing the source files in the web pages directory of a web server on your own system and viewed by entering http://localhost/http-request.html in your browser address field - assuming your server alias is "localhost" as usual.

*/
window.onload=show_status;

var http_request = false;								// global variable for HTTPRequest									
// function to request text from a specified url...
function make_request()									// execute an HTTPRequest 
{								
	if (window.XMLHttpRequest)							// create an HTTPRequest object...	
	{										// for IE7, Mozilla, Safari ...
		http_request = new XMLHttpRequest();				
	}
	else if (window.ActiveXObject)							// or create an HTTPRequest object...
	{										// for Internet Explorer 6...
		http_request = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else return false;								// or quit...

	http_request.open("GET", "data.txt", true);					// make the request to the server...
        http_request.send(null);							// stating method, filename, async
           
	http_request.onreadystatechange = show_status;					// nominate a response handler...

}


function show_status()
{
  window.document.forms[0].elements[0].value = http_request.readyState;
  window.document.forms[0].elements[1].value = http_request.status;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?