18.02.2015 Views

5ml User's Guide

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 8: Customizing Page Elements<br />

The Behavior tab on the Properties pane lets you associate an element (or page) with an event and an action to perform<br />

when the user taps or clicks the element. An action can execute a Custom Function that calls the <strong>5ml</strong> Proxy API. The API<br />

in turn can invoke a remote web service, hosted on another server. Use the Behavior tab's Script Editor to implement the<br />

custom code and <strong>5ml</strong> Proxy API. Then associate the custom code with a <strong>5ml</strong> widget (for example, a Button widget) by<br />

setting up an Event/Action sequence, using the Custom Function-Action and selecting the custom function from the<br />

Function drop down menu. For more information about setting up Events and Actions, see "Changing Behavior<br />

Properties" on page 223.<br />

<strong>5ml</strong> Proxy API Reference<br />

fiveml.proxy.sendRequest = function(url, httpMethod /*, data, oncomplete, headers*/);<br />

Parameters<br />

• url (required)—The remote web service URL that you want to invoke.<br />

• httpMethod (required)—The HTTP method to invoke the remote web service (GET or POST).<br />

• data (required for POST requests)—The data parameter can be of any type, depending on the web service you<br />

wish to invoke. If you want to invoke a web service using a GET HTTP request and you want to pass parameters<br />

as a query string, then pass a JavaScript object and the <strong>5ml</strong> Proxy API will manage the encoding.<br />

• oncomplete (optional)—A callback function to invoke after the remote service sends the response. It accepts one<br />

parameter, which is the XHR instance.<br />

• headers (required for POST)—A JavaScript object that contains headers that you want to set when invoking the<br />

remote web service. For POST HTTP requests, you should at least set the Content-Type header, as the API uses it<br />

to determine how to encode the POST data.<br />

The headers parameter can take the following three static variables:<br />

fiveml.proxy.TYPE_JSON = 'application/json';<br />

fiveml.proxy.TYPE_XML = 'text/xml';<br />

fiveml.proxy.TYPE_FORM_POST = 'application/x-www-form-urlencoded';<br />

<strong>5ml</strong> Proxy API Examples<br />

Sending a Simple GET HTTP Request<br />

function simpleGet() {<br />

fiveml.proxy.sendRequest('http://wsf.cdyne.com/WeatherWS/Weather.asmx/<br />

GetCityForecastByZIP','GET', null, function(req){alert(req.responseText);<br />

});<br />

Sending a GET HTTP Request with Query String Parameters<br />

function get() {<br />

- 228 -

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!