forked from wchaowu/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
arthinking
committed
Nov 7, 2012
1 parent
1f5854c
commit 00d63c9
Showing
3 changed files
with
67 additions
and
3 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<html> | ||
<head> | ||
|
||
</head> | ||
<body> | ||
<script> | ||
/* AjaxHandler interface. */ | ||
|
||
// var AjaxHandler = new Interface('AjaxHandler', ['request', 'createXhrObject']); | ||
|
||
/* SimpleHandler class. */ | ||
|
||
var SimpleHandler = function() {}; // implements AjaxHandler | ||
SimpleHandler.prototype = { | ||
request: function(method, url, callback, postVars) { | ||
var xhr = this.createXhrObject(); | ||
xhr.onreadystatechange = function() { | ||
if(xhr.readyState !== 4) return; | ||
(xhr.status === 200) ? | ||
callback.success(xhr.responseText, xhr.responseXML) : | ||
callback.failure(xhr.status); | ||
}; | ||
xhr.open(method, url, true); | ||
if(method !== 'POST') postVars = null; | ||
xhr.send(postVars); | ||
}, | ||
createXhrObject: function() { // Factory method. | ||
var methods = [ | ||
function() { return new XMLHttpRequest(); }, | ||
function() { return new ActiveXObject('Msxml2.XMLHTTP'); }, | ||
function() { return new ActiveXObject('Microsoft.XMLHTTP'); } | ||
]; | ||
|
||
for(var i = 0, len = methods.length; i < len; i++) { | ||
try { | ||
methods[i](); | ||
} | ||
catch(e) { | ||
continue; | ||
} | ||
// If we reach this point, method[i] worked. | ||
this.createXhrObject = methods[i]; // Memoize the method. 记住该方法 | ||
return methods[i](); | ||
} | ||
|
||
// If we reach this point, none of the methods worked. | ||
throw new Error('SimpleHandler: Could not create an XHR object.'); | ||
} | ||
}; | ||
|
||
/* Usage. */ | ||
|
||
var myHandler = new SimpleHandler(); | ||
var callback = { | ||
success: function(responseText) { alert('Success: ' + responseText); }, | ||
failure: function(statusCode) { alert('Failure: ' + statusCode); } | ||
}; | ||
myHandler.request('GET', 'script.php', callback); | ||
|
||
</script> | ||
</body> | ||
</html> |