06-28-2010 05:20 AM
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
06-28-2010 07:19 AM
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:
They are much lighter weight and will get the job done.
07-01-2010 06:35 AM
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.override MimeType('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=getInvoi ces&...",
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.
08-26-2010 01:52 PM
so can I call secure ws from my widget ?
08-26-2010 01:55 PM
By secure web service are you talking about a webservice that is using an SSL certificate that you communicate to via https?
08-26-2010 02:07 PM
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
08-26-2010 02:16 PM
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
08-26-2010 03:35 PM
sorry but what u mean by per user authentication based on an existing security table?
08-27-2010 09:05 AM - edited 08-27-2010 09:05 AM
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.
08-27-2010 03:51 PM - edited 08-27-2010 03:51 PM
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/GetStudentMarksWSSoa
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/GetStudentMarksWSSoa pHttpPort/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 


and I did alot of reasearch