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

Native Development

Reply
Highlighted
New Developer
Posts: 1
Registered: ‎03-18-2013
My Device: BB10
My Carrier: Vodacom

Error when getting JSON data from REST services

[ Edited ]

Hi,

I’m trying to GET JSON data from REST service then parse the JSON data.

I have managed to do it on a browser but, I seem to have blatantly failed

to do the same with Cascades.

 

var obj = {};
//var resp = "";
//Create XMLHttpRequest Objects
function createRequest(){
  var result = null;
  result = new XMLHttpRequest();
  return result;
};
        
//Use XMLHttpRequest
var req = createRequest(); // defined above
        
//Create the callback
req.onreadystatechange = function() {
  if (req.readyState != 4) return; // Not there yet
    if (req.status != 200) {
      // Handle request failure here...
        return;
    }
  };
        
  var GetData = function(url){
  //Send the Request
  req.open("GET", url, true);
  req.send();
        	
  //Request successful, read the response
  resp = req.responseText;  // Non-parsed JSON response
        	
  //... and use it as needed (Parse it)
  //obj = JSON && JSON.parse(resp) || $.parseJSON(resp);
  //obj = eval(resp);
  obj = JSON.parse(resp);
        	
  return obj;
  //return resp;
};

var data = GetData("http://app.compleatgolfer.co.za/service?view=provinces&token=gxRT67ALqp");

This works on IE browser but not on Cascades.

 When i try 

console.log(rep.status)

 or 

console.log(rep.responseText)

 I get "undefined"

 

Any help would be apreaciated.

 

Note: The code above, in my project, start on the 3rd line of the code below:

Page {
    onCreationCompleted: {

    }
}

 

 

Thanks

New Developer
Posts: 4
Registered: ‎08-31-2015
My Device: Developer
My Carrier: Developer

Re: Error when getting JSON data from REST services

Found something useful which people can try out to solve this (link below).

Apparently "if you don't check that the request readyState is DONE before querying the request status then you get 'Invalid state' errors".

So you should do this in this case:

if(req.readyState === XMLHttpRequest.DONE){
      if (request.status === 200) {
           //'On success' logic here         
      } else {
           //'On fail' logic here
      }
}


Credit goes to 

http://supportforums.blackberry.com/t5/Native-Development-Knowledge/Using-XMLHttpRequest-from-QML-in...

Developer
Posts: 259
Registered: ‎01-08-2013
My Device: Z10 LE
My Carrier: SiMobil

Re: Error when getting JSON data from REST services

Or use DataSource, works perfectly, less code

 

attachedObjects: [
            DataSource {
                id: dataSource
                source: "http://app.compleatgolfer.co.za/service?view=provinces&token=gxRT67ALqp"
                type: DataSourceType.Json
                onDataLoaded: {
                    console.log(JSON.stringify(data));
                }
            }
        ]