This page gives a few examples of how to use the Spry.Utils.loadURL() to send "GET" and "POST" requests to the server. The Spry.Utils.loadURL() function is currently located in SpryData.js, so before you use it, you must include it:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> ... <script src="../../includes/SpryData.js" type="text/javascript"></script> ... </head>
Before proceeding any further, let's take a look at the arguments for the loadURL function:
var req = Spry.Utils.loadURL = function(method, url, async, callback, opts);
The arguments are as follows:
function MyCallback(req) { // Do your processing here. }
When the callback is triggered, the Spry.Utils.loadURL.Request object is passed to the callback. See below for more details.
// Example of creating a header object the literal way: var header = { "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8" }; // Example of creating a header object manually: var header = new Object; header["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
The return value of loadURL is an object of type Spry.Utils.loadURL.Request. This is the same object that will be past into any callbacks you've passed into loadURL. The fields in this object look just like options you pass into loadURL, but it also contains fields that contain the arguments passed into the loadURL call.
Here's a list of the properties on the Request object.
Now that we know all about loadURL, lets take a look at a few sample calls.
In this example we just want to do an asynchronous "GET" request, but we don't care about the results, so we don't register a callback.
var req = Spry.Utils.loadURL("GET", "/app/book.php?id=1&code=54321", true);
In this example, we want to show an alert if the request completes successfully.
function MySuccessCallback(req) { alert(req.url + " completed successfully!"); } ... var req = Spry.Utils.loadURL("GET", "/app/book.php?id=1&code=54321", true, MySuccessCallback);
function MySuccessCallback(req) { // Throw an alert with the message that was // passed to us via the userData. alert("SUCCESS: " + req.userData.msg); } ... // Create some data to be sent along with the request. // This really can be anything you want, but I'm going to // use an object. var myObj = new Object; myObj.msg = "Hello World"; var req = Spry.Utils.loadURL("GET", "/app/book.php?id=1&code=54321", true, MySuccessCallback, { userData: myObj });
var req = Spry.Utils.loadURL("POST", "/app/book.php?id=1&code=54321", true, null, { postData: "action=update&genre=fiction", headers: { "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8" } });
function MySuccessCallback(req) { // Throw an alert with the message that was // passed to us via the userData. alert("SUCCESS: " + req.userData.msg); } function MyErrorCallback(req) { // Throw an alert with the message that was // passed to us via the userData. alert("ERROR: " + req.userData.msg); } ... // Create some data to be sent along with the request. // This really can be anything you want, but I'm going to // use an object. var myObj = new Object; myObj.msg = "Hello World"; var req = Spry.Utils.loadURL("POST", "/app/book.php?id=1&code=54321", true, MySuccessCallback, { postData: "action=update&genre=fiction", headers: { "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8" }, errorCallback: MyErrorCallback });