229

My script is printing [object Object] as a result of console.log(result).

Can someone please explain how to have console.log print the id and name from result?

$.ajaxSetup({ traditional: true });

var uri = "";

$("#enginesOuputWaiter").show();    
$.ajax({
    type: "GET",
    url: uri,
    dataType: "jsonp",
    ContentType:'application/javascript',
    data :{'text' : article},
    error: function(result) {
        $("#enginesOuputWaiter").hide();
        if(result.statusText === 'success') {
            console.log("ok");
            console.log(result);
        } else {
            $("#enginesOuput").text('Invalid query.');
        }
    }
});
SuperStormer
  • 4,997
  • 5
  • 25
  • 35
Wassim Benhamida
  • 2,377
  • 2
  • 10
  • 9
  • 2
    Did you try parsing it? `[object Object]` is just a string representation, not what the object actually looks like. – Carcigenicate Dec 26 '16 at 22:06
  • That webservice seems to return JSON not JSONP, so you should change your `dataType`. That's probably also why your **`error`** handler is invoked, where you hardly will get a successful `result`. – Bergi Dec 26 '16 at 22:08
  • You've BOM symbol in Your response: http://joxi.ru/vAWVVbh1k34N2W so fix Your php code, cleanup unnecessary utf8 encoding BOMs – num8er Dec 26 '16 at 22:09
  • 5
    `result.statusText = 'success'` should be `result.statusText === 'success'`. Otherwise you are assigning 'success' to the result object. – Andrés Andrade Dec 26 '16 at 22:17
  • could You explain the necessarity of this in `get` request: `data :{'text' : article}` ? – num8er Dec 26 '16 at 22:24
  • use console.dir() instead of console.log() – smoore4 Sep 17 '20 at 13:24
  • 5
    Try ````console.log("result: ", result);```` – mtotowamkwe Jul 15 '21 at 22:23
  • @SuperStormer Why are you fixing the code? OP is trying to test for success in the error callback and the wrong operator is making it seem like the request succeeded. – gre_gor Oct 02 '22 at 02:22
  • @gre_gor because it's irrelevant to the actual question and answers – SuperStormer Oct 02 '22 at 18:20
  • 1
    I cast a reopen vote because the question is about how to print a JS object, completely unrelated to the dupe target. – SuperStormer Oct 02 '22 at 18:22
  • @mtotowamkwe hi, Thank you very much for the answer. Can you please extend your answer with more explanation about the reason it works? – barper Oct 22 '22 at 07:56
  • @barper Mozilla documentation says if you log objects use console.log(JSON.parse(JSON.stringify(obj))); instead of console.log(obj); I believe it works because the console.log() API passes the object you provide to a Formatter function which parses the object keys and object values and prints them to the console. See:- https://developer.mozilla.org/en-US/docs/Web/API/Console/log I think there should be information in the JavaScript spec. If I dig anything up I'll update this comment accordingly. Note the result returned is specified as "implementation-defined". – mtotowamkwe Oct 25 '22 at 23:49
  • This should absolutely be reopened. The answer may be the same as the one marked duplicate but the question is entirely different. A user (like the OP) might not be working with an asychronous call at all and still have this very same problem. I see @SuperStormer has made a similar comment above. – Raydot Feb 10 '23 at 17:30
  • The answer isn't even the same... – SuperStormer Feb 11 '23 at 07:44

3 Answers3

287

Use console.log(JSON.stringify(result)) to get the JSON in a string format.

EDIT: If your intention is to get the id and other properties from the result object and you want to see it console to know if its there then you can check with hasOwnProperty and access the property if it does exist:

var obj = {id : "007", name : "James Bond"};
console.log(obj);                    // Object { id: "007", name: "James Bond" }
console.log(JSON.stringify(obj));    //{"id":"007","name":"James Bond"}
if (obj.hasOwnProperty("id")){
    console.log(obj.id);             //007
}
suvartheec
  • 3,484
  • 1
  • 17
  • 21
  • 3
    You can also get json style log in this manor 'console.log('connection : %j', myObject);' – Chinmay Samant Nov 09 '18 at 06:53
  • 11
    Hi, I thought Im trying to do the same thing but I kept get getting `ERROR TypeError: Converting circular structure to JSON` . Is this because Im applying this method not on an object? – Gel Jan 18 '19 at 02:31
  • 3
    @GelSisaed That will happen if any of the property of your variable is a reference to that variable. refer [this post](https://stackoverflow.com/a/50773821/1304575) and also [this one](https://stackoverflow.com/a/4816258/1304575) – suvartheec Jan 18 '19 at 09:26
  • 23
    Yup stringifying gave me `"[object Object]"` like it's mocking me. – Ben Racicot Oct 22 '20 at 21:07
  • 1
    You can also use `console.debug(obj)` or console.dir(obj) to get the result in nodejs – ikhvjs Aug 25 '21 at 09:16
69

Try adding JSON.stringify(result) to convert the JS Object into a JSON string.

From your code I can see you are logging the result in error which is called if the AJAX request fails, so I'm not sure how you'd go about accessing the id/name/etc. then (you are checking for success inside the error condition!).

Note that if you use Chrome's console you should be able to browse through the object without having to stringify the JSON, which makes it easier to debug.

3

Try with:

console.dir(object)

This is the way to see all the properties of a specified JavaScript object in console.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
cris.sol
  • 161
  • 1
  • 1
  • 10