0

I have the following code in my client which is executed on page load. My client pages and JS are hosted by mode my Node.js script with Express

    initHomePage : function(options) {
        $.getJSON('siteinfo.json',
             function(data) {
              //called when complete
              alert('process complete' + data);
             });    
    },

Which is calling the following Node.js

exports.get_site_setup = function (req, res) {
var dbc;

async.waterfall([
    // get a connection

    function (callback) {
        db.db(callback);
    }

    ,querylookup    
], completed);

function querylookup(dbclient, callback) {
    dbc = dbclient;
    dbc.query("SELECT site_id, "+
                "name, "+
                "quicklist, "+
                "image, "+
                "image2, "+
                "message, "+
                "fbflag, "+
                "facebookurl, "+
                "twflag, "+
                "twitterurl, "+
                "contactflag, "+
                "contactemail, "+
                "eventsflag, "+
                "loyaltyflag, "+
                "loyaltyclub, "+
                "loyaltymessage, "+
                "location_id, "+
                "locdescription "+
                "FROM LOCATION_LOOKUP_ACTIVE_VIEW "+
                "where site_id = 7 and location_id = 8",
            callback); 
}

function completed (err, rows, fields) {
    if (dbc) dbc.end();
    if (err) {
        callback (err);
    } else {
        console.log(rows[0]);
        res.contentType('json');
            res.send(JSON.stringify(rows[0]));
    }

}
};

My console.log is firing and showing my expected JSON in the console when I expect the the request to be made.

However the alert box that pops up in my browser says

"process complete[object Object]"

Why is my JSON not passing from my server side to my client script?

0

2 Answers 2

3

The JSON you send back is being parsed into a JavaScript object. If instead of an alert you did

console.dir(data);

it'd be clearer.

That's kind-of the whole point of $.getJSON().

Sign up to request clarification or add additional context in comments.

2 Comments

I'm only doing an alert to test that I am indeed receiving the proper JSON before I do something with it.
His point is that data is no longer a JSON string when you attempt to alert() it, it's already been parsed into a native javascript object. So, what you're really doing is this: alert('process complete'+data.toString())... and when you .toString() an object literal, you get [object Object]
1

To expand on what @Pointy says, 'data' that you are trying to show in the alert should be the JSON response. Try stringify as below if you want to see it in alert :

alert('process complete ' + JSON.stringify(data));

3 Comments

OMG..lol that completely didn't even occur to my brain...thanks Guys!
To continue on with this discussion..do I then reference to it as JSON in my client scripts just as I would normally?
@ddpishere well what your function gets is a full-blown JavaScript object. The concept of "JSON" Is really more of a serialization scheme; once it's been parsed and turned into a JavaScript object, it's just an ordinary object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.