Last active
December 26, 2015 20:39
-
-
Save crazygit/7209754 to your computer and use it in GitHub Desktop.
an example of send an ajax request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nocache = "&nocache=" + Math.random() * 1000000 | |
request = new ajaxRequest() | |
request.open("GET", "urlget.php?url=oreilly.com" + nocache, true) | |
// 与post相比, get 请求不需要设置header | |
request.onreadystatechange = function() | |
{ | |
if (this.readyState == 4) | |
{ | |
if (this.status == 200) | |
{ | |
if (this.responseText != null) | |
{ | |
document.getElementById('info').innerHTML = this.responseText | |
} | |
else alert("Ajax error: No data received") | |
} | |
else alert( "Ajax error: " + this.statusText) | |
} | |
} | |
request.send(null) | |
function ajaxRequest() | |
{ | |
try | |
{ | |
var request = new XMLHttpRequest() | |
} | |
catch(e1) | |
{ | |
try | |
{ | |
request = new ActiveXObject("Msxml2.XMLHTTP") | |
} | |
catch(e2) | |
{ | |
try | |
{ | |
request = new ActiveXObject("Microsoft.XMLHTTP") | |
} | |
catch(e3) | |
{ | |
request = false | |
} | |
} | |
} | |
return request | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
params = "url=oreilly.com" | |
request = new ajaxRequest() | |
// Specifies the HTTP method to use (GET or POST), the target URL, and | |
// whether the request should be handled asynchronously (true or false) | |
request.open("POST", "urlpost.php", true) | |
// Sets a header with a parameter/value pair | |
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded") | |
request.setRequestHeader("Content-length", params.length) | |
request.setRequestHeader("Connection", "close") | |
//Specifies an event handling function to be called whenever the readyState property of an object changes. | |
request.onreadystatechange = ajaxCallBack | |
// Sends data to the target server using the specified HTTP method | |
request.send(params) | |
function ajaxCallBack() | |
{ | |
// An integer property that reports on the status of a request. | |
// It can have any of these values: | |
// 0 = Uninitialized, 1 = Loading, 2 = Loaded, 3 = Interactive, and 4 = Completed. | |
if (this.readyState == 4) | |
{ | |
// The HTTP status code returned by the server. | |
if (this.status == 200) | |
{ // The data returned by the server in text format. | |
if (this.responseText != null) | |
{ | |
document.getElementById('info').innerHTML = | |
this.responseText | |
} | |
else alert("Ajax error: No data received") | |
} | |
// The HTTP status text returned by the server. | |
else alert( "Ajax error: " + this.statusText) | |
} | |
} | |
function ajaxRequest() | |
{ | |
try // Non-IE browser? | |
{ | |
var request = new XMLHttpRequest() | |
} | |
catch(e1) | |
{ | |
try // IE 6+? | |
{ | |
request = new ActiveXObject("Msxml2.XMLHTTP") | |
} | |
catch(e2) | |
{ | |
try // IE 5? | |
{ | |
request = new ActiveXObject("Microsoft.XMLHTTP") | |
} | |
catch(e3) // There is no Ajax support | |
{ | |
request = false | |
} | |
} | |
} | |
return request | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment