0

Just a fast question.

I'm using ajax POST and GET to send json data to a server and retrieve that data back. However, what I'm confused about is getting json info from the GET call.

getMessage = function(){
  $.ajax({
      url:'/get_messages',
      dataType: 'json',
      type: 'GET',
      success: function(msg){
          console.log("got eeeem " + msg );
      }
  });
};

What i've seen so far implies what i'm getting from the server should be displayed in msg. However in the console log, msg is displayed as "got eeeem [object Object]". So I'm confused as to how to pull the necessary information out of the msg.

My post looks like this:

var packet = {
  'username': 'Name',
  'message': innerText,
  'date': new Date().toUTCString()
};
$.ajax({
    url:'/receive_message',
    data: JSON.stringify(packet),
    type: 'POST',
    success: function(msg){
        console.log("EUREKA " + msg);
    }
});

Specifically from the GET call, I want to retrieve the innerText variable from the json. Any help is much appreciated

Ashish Bahl
  • 1,482
  • 1
  • 18
  • 27
  • What are you returning from the server? – TheValyreanGroup Mar 06 '17 at 23:34
  • What is the actual object returned from the endpoint? Its possible you need to access a property on the object. Like msg.SomeProperty. – Ju66ernaut Mar 06 '17 at 23:34
  • 1
    Try `console.log('got eeeem', msg)`. Using string concatenation (ie `'some string' + someVar`), you are getting the string version of an object, ie `[object Object]` – Phil Mar 06 '17 at 23:36
  • Should've been more specific. Json is being sent and received – ACarlton Mar 06 '17 at 23:39
  • Also, if you want to POST JSON, you should set the right headers, `contentType: 'application/json'` – Phil Mar 06 '17 at 23:43
  • switched to console.log('got eeeem', msg) and it's now saying i'm getting an array of length 34 filled with nulls which it shouldn't be. @Phil is application/json universal or is application a placeholder in this instance – ACarlton Mar 06 '17 at 23:46
  • @ACarlton if it's full of nulls, then that's what your server is returning. You can confirm by looking in your browser's *Network* console at the **response** data for your AJAX requests. Also, see http://stackoverflow.com/questions/6255344/how-can-i-use-jquery-to-post-json-data – Phil Mar 06 '17 at 23:49
  • @Phil interesting. I just applied the contentType addition and while im still getting an array of nulls. the final element is the json i originally sent. – ACarlton Mar 06 '17 at 23:51
  • So i'm slightly confused as to why the server would be sending back nulls with the desired info being in the final index – ACarlton Mar 07 '17 at 00:01

1 Answers1

1

Your are receiving Json object from server in ajax success.You need to convert that object to string

Use like this JSON.stringify(msg);

    var packet = {
      'username': 'Name',
      'message': innerText,
      'date': new Date().toUTCString()
    };
    $.ajax({
        url:'/receive_message',
        data: JSON.stringify(packet),
        type: 'POST',
        dataType:'application/json',
        success: function(msg){
            console.log("EUREKA " + JSON.stringify(msg));
        }
    });
JRA
  • 467
  • 5
  • 18