Вы находитесь на странице: 1из 4

Ret ur n t o Aj ax

Tags Applied to this Topic


1 person has tagged this page:
Ajax code HOWTO ayo
Tag this Topic | Tagging Details
Ajax Wiki Pages
Places To Use Ajax
Ajax Development Links
How to use XMLHttpRequest
Ajax Images
Ajax-Development-Gotchas
AJAX/PHP Based Chat System
See all 14 Pages
Recent Edits for Ajax
Recent Edits for How to use XMLHttpRequest
Subscribe RSS
How to use XMLHttpRequest
Wiki Page
Sample javascript implementing a cross platform xmlhttprequest function:
Method 1 Public domain XMLHttpRequest Object
1
Method 2 XHConn, CC-SA XMLHttpRequest Object
2
Returning Content
Through XMLHttpRequest
3
Method 1
In this method, you can instantiate an XMLHttpRequest object that can then
call functions from the server. The code is in the public domain.
function getNewHTTPObject()
{
var xmlhttp;
/** Special IE only code ... */
/*@cc_on
@if (@_jscript_version >= 5)
try
{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (E)
{
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
/** Every other browser on the planet */
if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
{
try
{
xmlhttp = new XMLHttpRequest();
}
catch (e)
{
xmlhttp = false;
}
}
return xmlhttp;
}
Now you can call XMLHTTPRequest
Ajax: How to use XMLHttpRequest http://swik.net/Ajax/How+to+use+XMLHttpRequest
1 de 4 19/08/2010 02:41
var xmlHttp = getHTTPObject();
function getDynamicData()
{
var url = "http://url/that/returns/dynamic/content";
xmlHttp.open('GET', url, true);
xmlHttp.onreadystatechange = callbackFunction;
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlenco
xmlHttp.send(null);
}
The callback function set in xmlHttp.onreadystatechange will be called when
data is back from the server
var syndLinkRequest = getNewHTTPObject();
function callbackFunction()
{
if (syndLinkRequest.readyState != 4)
return;
var result = xmlHttp.responseText;
/* if you've returned javascript instead of xml or text,
you can eval(result) to access the javascript variables returned.
*/
}
Method 2 XHConn
XHConn is an open source project that attempts to create a very simple and
cross platform way to invoke Ajax requests
Ajax: How to use XMLHttpRequest http://swik.net/Ajax/How+to+use+XMLHttpRequest
2 de 4 19/08/2010 02:41
/** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08
** Code licensed under Creative Commons Attribution-ShareAlike License
** http://creativecommons.org/licenses/by-sa/2.0/
function XHConn()
{
var xmlhttp, bComplete = false;
try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
catch (e) { try { xmlhttp = new XMLHttpRequest(); }
catch (e) { xmlhttp = false; }}}
if (!xmlhttp) return null;
this.connect = function(sURL, sMethod, sVars, fnDone)
{
if (!xmlhttp) return false;
bComplete = false;
sMethod = sMethod.toUpperCase();
try {
if (sMethod == "GET")
{
xmlhttp.open(sMethod, sURL+"?"+sVars, true);
sVars = "";
}
else
{
xmlhttp.open(sMethod, sURL, true);
xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
xmlhttp.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
}
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4 && !bComplete)
{
bComplete = true;
fnDone(xmlhttp);
}};
xmlhttp.send(sVars);
}
catch(z) { return false; }
return true;
};
return this;
}
Using XHConn:
//initialize XHConn (if XHConn isn't created successfully,
//the client doesnt' support Ajax)
var ajaxConn = new XHConn();
//post to mypage.php with args foo and baz
ajaxConn.connect("mypage.php", "POST",
"foo=bar&baz=qux",fnWhenDone);
//when the server responds, javascript
//will trigger this callback function
fnWhenDone(XML)
{
alert(XML.responseText);
}
Returning content through the XMLHTTPRequest method:
There are various ways you can return content through XMLHTTPRequest:
Plain text (XML.responseText)
Javascript variables (eval(XML.responseText))
JSON Javascript objects (eval(XML.responseText))
XML (XML.responseText = XML)
Of these, plain text is the simplest, as all it requires is servicing a request with a
simple plain text dump of data. The downside to this method is that its not
always a clean way to return more than one variable.
Ajax: How to use XMLHttpRequest http://swik.net/Ajax/How+to+use+XMLHttpRequest
3 de 4 19/08/2010 02:41
Javascript variables are a nice way to return multiple variables through an
XMLHTTPRequest call. Instead of dumping plain text, the server decorates the
different returned variables with javascript variable declarations. On the client
end, the XMLHTTPRequest callback function evals the server response and
now has the variables set by the server available to it as native javascript
variables.
One key part of this method however is remembering to escape the content in
the javascript variable assignment. link
The third way of passing back content through XMLHTTPRequest is XML,
which despite the name of the function, is often the slowest and most difficult
way due to difficulties parsing xml. However for highly structured content or
when E4X becomes more standard, this might be the way to go.
About SWiK Random Project Recent Edits Zeitgeist
All original text is available as Creative Commons Attribution-ShareAlike
2009 DZone, Inc.
Privacy. Terms. Credits.
Ajax: How to use XMLHttpRequest http://swik.net/Ajax/How+to+use+XMLHttpRequest
4 de 4 19/08/2010 02:41

Вам также может понравиться