Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Creating APIBridge JavaScript Binding Code

From Wiki
Jump to: navigation, search

This article shows how to create APIBridge Binding Code in JavaScript, making it possible to access device services in Symbian WebRuntime widgets.

Article Metadata
Created: User:Kbwiki (11 Feb 2010)
Last edited: hamishwillee (07 Nov 2011)


Binding JavaScript™ Code

The binding code for the EchoServlet example can be found in \bindings\js\sample.js. This file uses the APIBridge class, which is contained in the apibridge.js file that ships with APIBridge.

The code for this example is:

var Sample = {
echo: function( msg, err, onSuccess, onError) {
APIBridge.Internal._sendRequest( "/sample/echo?msg="+encodeURIComponent(msg) +"&err="+encodeURIComponent(err),
if (req.responseText)
onSuccess (req.responseText);
onError );

Essentially, the binding code translates the parameters passed into a URI-encoded GET string by using the APIBridge.Internal._sendRequest function. It then analyses the response from the framework and calls the appropriate callback.

If you dissect the URL portion, you will see two parts:

  • Request: /sample/echo. This request will get routed to your plug-in as specified in the ECOM Plug-in resource file.
  • Parameters: ?msg="+encodeURIComponent(msg)+"&err="+encodeURIComponent(err). The parameters are extracted at the servlet’s ServiceL function by using the RQueryParser class and this function:
     req-> GetRequest()->GetQuery()

How it is used

The code below shows how a Web Runtime (WRT) application might use this newly created function:

Sample.echo(msg,err, function(text) //OnSuccess callback
output.innerHTML += "The APIBridge returned: "+text+"<br/>";
function(err) //OnError callback
output.innerHTML += "Error: "+err.status + ". Message: "+err.responseText+"<br/>";

JavaScript Binding API

The binding API for JavaScript™ can be found in the apibridge.js file that ships with APIBridge.

SendRequest function

APIBridge.Internal._sendRequest: function( path, parameters, onSuccess, onError )

This function is used to send a raw request to the APIBridge server; it takes care of security and access control for the developer.

Parameter Description
path URI path to the plug-in including any GET parameters. The http://localhost part should NOT be included here.
parameters String that will be passed in the body of the HTTP request. If this parameter is not null, the request will be a POST request, otherwise it will be a GET.
onSuccess Callback function that returns the XMLHttpRequest object as the single parameter. It is called when the server reports a 200 HTTP status code.
onError Callback function that returns the XMLHttpRequest object as the single parameter. It is called when the server reports an HTTP status code other than 200, the access is not granted, or the path is not associated with any plug-in.
This page was last modified on 7 November 2011, at 04:21.
38 page views in the last 30 days.