Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
New Developer
sibbaldl
Posts: 41
Registered: ‎06-22-2010
My Device: N/A

Ajax and Widgets

Hi Everyone

 

This is my second posting to the forum and it's another noob one I'm afraid.

 

My question is regarding AJAX in BB Widgets. I'm targeting devices running BB 5.0 and up only, and I'm currently putting together the infrastructure at the server side to support the widget operation. 

 

My original intention (or preference) was to return pre-formatted HTML responses to the widget, so that the widget could simply insert the html directly in to the DOM. However, I later decided that this would mean regular amounts of redundant data going across to the device over the air - hardly ideal in a wireless model. I therefore decided to return JSON.

 

So far I've built a .net page to return a simple json [ {}, {}, ... ] response and now I'm wondering how to best handle this in the bb widget.

 

I've previously tried to use jQuery but I was unable to even get the ready function to fire. I included simple ready script in the body of the widget's index.html, and referenced the jQuery js file in the head section, but when i ran the widget I didnt see the test alert I'd inserted. Note that I wasn't making any external calls, I was simply just trying to test for jQuery initialisation.

 

So my question is, can we use jQuery in BB widgets (I'm sure we can) and if so, how does one go about including the .js files? Are there any big fat gotchas that I should know about before going too far down the road with jQuery? Does Google Gears or the built-in XHR object provide a better alternative (if so how do we handle json)?

 

If not, what is the recommended approach to making ajax calls and processing json responses?

 

Many thanks

 

Lee

=============================
Win 7 (32bit)
Eclipse Galileo
BlackBerry Web Plug-in
Version: 2.0.0.201003191451-33
BlackBerry Widget SDK
Version: 1.0.0.201003191451-126
=============================
Retired
tneil
Posts: 3,708
Registered: ‎10-16-2008
My Device: Z10
My Carrier: Rogers

Re: Ajax and Widgets

Hi Lee,

 

JQuery has limited success on the BlackBerry Browser.  It is a very big JavaScript toolkit that isn't very efficient in the mobile arena.

 

I would suggest using one of the JavaScript JSON parsers found at:

 

http://json.org/

 

They are much lighter weight and will get the job done.

Tim Neil
Director, Application Platform & Tools Product Management
Follow me on Twitter
Developer
confinedspace
Posts: 40
Registered: ‎04-16-2010
My Device: na

Re: Ajax and Widgets

I don't use jQuery in my widgets. I ended up writing this function to make my ajax calls:

 

 

function ajax(m,u,f){var h,r,i;h=new XMLHttpRequest();if(h.overrideMimeType){h.overrideMimeType('application/json, text/javascript');}h.onreadystatechange=function(){if(h.readyState==4 && h.status==200){r=eval("("+h.responseText+")");f(r);}};h.open(m,u,true);h.send(null);}

 

Params:

m = method ("POST" or "GET")

u = url ("http://www.mydomain.com/.../.../")

f = callback function (e.g. outputResults)

 

It uses the javascript "eval" function to assign the JSON response to a return variable. As widgets can only call URI's specified in the config.xml (in the "access" tag) then there should be no danger of getting a result from somewhere evil.

 

An example call would be (I'm using ColdFusion components - but this won't make a different to your client side code):

 

ajax('POST',
    "https://mydomain.com/bbapp/App.cfc?method=getInvoices&...",
    outputInvoices
);

 

To handel the response, create a function called "outputInvoices" (which is the callback function passed above):

 

function outputInvoices(r){

}

 

Depending on the structure of your JSON, you can access the data with "r.", e.g. "r.DATA.INVOICES".

 

Hope this helps, give kudos if it does.

 

If you can spot any improvements or problems let me know.

Contributor
missmanar1987
Posts: 32
Registered: ‎06-26-2010
My Device: all

Re: Ajax and Widgets

so can I call secure ws from my widget ?

Retired
tneil
Posts: 3,708
Registered: ‎10-16-2008
My Device: Z10
My Carrier: Rogers

Re: Ajax and Widgets

By secure web service are you talking about a webservice that is using an SSL certificate that you communicate to via https?

Tim Neil
Director, Application Platform & Tools Product Management
Follow me on Twitter
Contributor
missmanar1987
Posts: 32
Registered: ‎06-26-2010
My Device: all

Re: Ajax and Widgets

Thanks Tim for your quick replay really thanks

 

yes I have to call webservise that is using SSL (its URL Https:\....)

if I can ? How to authenticate my widget to access the ws ?  

thaaaanks for helping

Retired
tneil
Posts: 3,708
Registered: ‎10-16-2008
My Device: Z10
My Carrier: Rogers

Re: Ajax and Widgets

Yes you can communicate via SSL by simply using https on your URL and it will secure the channel when it handshakes with your web server.

 

As far as authenticating, are you looking at a per user authentication based on an existing security table?  Or are you trying to use NTLM/BASIC authentication

Tim Neil
Director, Application Platform & Tools Product Management
Follow me on Twitter
Contributor
missmanar1987
Posts: 32
Registered: ‎06-26-2010
My Device: all

Re: Ajax and Widgets

sorry but what u mean by per user authentication based on an existing security table?

 

Retired
tneil
Posts: 3,708
Registered: ‎10-16-2008
My Device: Z10
My Carrier: Rogers

Re: Ajax and Widgets

[ Edited ]

I was just referring to that most applications have their own application level security where there are user accounts for the system and they don't rely on a BASIC authentication or NTLM authentication.

 

They are session based and the login is sent to the application to verify and track.

Tim Neil
Director, Application Platform & Tools Product Management
Follow me on Twitter
Contributor
missmanar1987
Posts: 32
Registered: ‎06-26-2010
My Device: all

Re: Ajax and Widgets

[ Edited ]

I need help I am stoked for about 3 weeks in how can I call ws

the ws is reside in this URL :https://X.Y.Z.ta/testMobileWS/GetStudentMarksWSSoapHttpPort

it is using ssl

this ws contain method called

 

getStudentMarksXML( pStudentId decimal  ,pSemester decimal , pLang decimal)

 

and I want to call this method and the expected response is xmlElement

 

I did the following

in confg.xml in Specify a list of domains and features to which the widget requires access.

I add this domain https://X.Y.Z.Ta where my ws is

and check allow access to subdomain

 

then I wrote this code to call ws

 

function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
var id = 423101774 ;
var sem = 301;
var lang = 1;
var url = "https://X.Y.Z.Ta/testMobileWS/GetStudentMarksWSSoapHttpPort/getStudentMarksXML?pStudentId="+id+"&pSemester="+sem+"&pLang="+lang;

try{

ajaxRequest = new XMLHttpRequest();
} catch (e){

try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.innerHTML = ajaxRequest.responseXML.nodeName;
alert(ajaxRequest.responseText);
alert("res");
}
}

ajaxRequest.open("GET", url , true);
ajaxRequest.send(null);
}

 

 

but when I run it there is no respnse for ws calling

and

alert("res");

Not shown ever!!

 

what is the problem , and any suggestions

 

is it wrong to send function parameters in url ? if yes so how ?

 

Sir Tim and others  your response is higlllllllllllllllllllllllllly appreciated coz I swear to God I am stocked :smileysad::smileysad::smileysad:

and I did alot of reasearch