1

I am developing a restlet service and a front end. I exchange the info beetween server and client throught JSON objects, but i cant access to the data on the client side (JavaScript).

The Server creates a JSON object with the desired info:

final JSONObject response = new JSONObject();
JSONArray content = new JSONArray(list);
response.put("content", content);
return response.toString();
System.out.println(response.toString());
return response.toString();

When the service is called the syso prints:

{"content":["Hospital","Fabrica"]}

The JSON object is then sent to the client but it can“t access to the content record. The client code is the following:

UpRankApp.service('primaryUseService', ['$resource', 'globalService',  function($resource, globalService){

    this.getAvailablePrimaryUses = function(){

        var api = $resource("http://localhost:9192/getListPrimaryUse", 
        {
            callback: "JSON_CALLBACK" 
        },
        {
            post: {method: "JSON" }
        }
        );

        return globalService.getMessageContent(api.get());  
        //return [ 'saude', 'comercio', 'industria' ];

    };

}]);

UpRankApp.service('globalService', [function(){

    this.getMessageContent = function(message){

        console.log(message);
        console.log(message.content);

        if(message.hasOwnProperty('content')){
            console.log(message.content);
            return message.content
        }else if(message.hasOwnProperty('error')){
            console.log(message.error);
        }else{
         console.log("rien");   
        }   
    }
}]);

I can see the sent data in the client side trought the console.log on getMessageContent() but i cant access it. The console log output on firefox is the following:

Object { content: Array[2], $promise: Object, $resolved: true } undefined

With chrome is the same thing but the content record is not directly seen in the JSON object (but the data is there)

JSON object in chrome

I have already tried to convert the message to json using :

var data = JSON.parse(message); 
if (data.status == 'success') { console.log('IT WORKS!');}

but i get an error saying "Error: JSON.parse: unexpected character at line 1 column 2 of the JSON data" wich means the the variable is already a JSON object right? Does anyone have any idea of what happening? I am trying to find a solution for this problem for 3 days and i can't find a solution.

Best regards and thanks in advance!

JotaTRod
  • 91
  • 2
  • 7

0 Answers0