0

I have an object that contains messages that are of JSON type. I have a method called .retrieve() that uses an AJAX request to download messages from the server. It will fetch the messages from the server then call a callback function with an array of strings (messages).

.retrieve():

Msg.retrieve = function(callBack) {
    $.ajax({
      url : '<url>',
      type : 'GET',
      dataType: 'JSON',
      contentType : 'application/json',
      data : JSON.stringify({
        key : 'value: ' 
      }),
      error : function(data) {
        console.log('error');
      },
      success : function(data) {
          callBack(data);
      }
    });
  }

I have a .display() method that will output the messages as list items in an unordered list like a chat client would.

.display()

 Msg.display = function(text) { 
      if (typeof text == "undefined"){
        $('.chat').append('<li>' + "" + '</li>');
      } else {
        $('.chat').append('<li>' + text + '</li>');
      }
  }

When I combine .display() and .retrieve() it gives me [object Object] as a list item in my browser.

Question: How do I output the messages rather than displaying [object Object] on my browser?

andy4thehuynh
  • 2,042
  • 3
  • 27
  • 37
  • How are you calling them? What does the server return? – SLaks Mar 24 '13 at 02:20
  • 2
    So apparently `text` is an object. Instead of performing string concatenation on the object itself, access one of its properties (the one you want). See: http://stackoverflow.com/q/11922383/218196. – Felix Kling Mar 24 '13 at 02:21
  • @SLaks I get [object Object] on my browser as a list item. I want to access that object's property called text. – andy4thehuynh Mar 24 '13 at 02:36
  • @leggooo: If text is a property in that object then you can simply access by writing object.text. But what are you returning in the server end? – karthick Mar 24 '13 at 02:38
  • @leggooo try adding `console.log(data);` in the `success` function to see what the properties of that object are. – rsbarro Mar 24 '13 at 03:39

0 Answers0