Friday, June 3, 2011

AS3 Code - Posting to web service / External Script

Hello,

Todays code snippet can be accessed using the below url:
Script Loader class: http://paste.hostingforoz.com/index.php/view/4561f5d0

Daniel Event Class: http://paste.hostingforoz.com/index.php/view/12162fc1

Description: Class to access and send data to an external script
This class can be used to easily access external scripts and web services, with just a few lines.
Heres an example:


// Create a new instance of the Script Loader.. Using the POST method and the default data type
var sl:ScriptLoader = new ScriptLoader("http://site.tld/webservicescript.php");

// Add a variable to be sent with the request..
sl.addVariable("aVariable", "testScriptLoader");

// Listen for the "on Completed" event, to notify the function "onSessionCheck" when the web service has been loaded..
// DanielEvent is a custom Event class which needs to be included in the project
sl.addEventListener(DanielEvent.COMPLETED, onSessionCheck);

// Finally load the service/script
sl.load();

function onSessionCheck(e:DanielEvent):void {
var data:Object = e.data as Object;
// data now contains the response data in an object.
if(data.myVar != null) trace(data.myVar);
}

A few notes:

The web service script could reply something like "myVar=myValue&anotherVar=anotherValue" and you could then access that using "data.myVar".

No comments:

Post a Comment