javascript - Unable to fetch object value -
i struggling figure out why getting "undefined" when trying fetch object value.
my object, called "message" looks this:
{"username":"123","password":"123"}
when try fetch either username or password following:
message.username message['username']
i "undefined".
what's correct way fetch values?
code gets message , displays it:
window.messagebus.onmessage = function(event) { displaytext(event.data); // spits out {"username":"123","password":"123"} } function displaytext(message) { document.getelementbyid("message").innerhtml = message.username + ' - ' + message.password; };
edit - object being received chromecasts "window.messagebus.onmessage" function. assumed because sent message proper object, should have bene spat proper object, seems might not case!
well, no idea why wasn't working code posted, means should have.
anyway, fixed using "json.parse()" on "message" variable passed "displaytext()"
i able use "message.username" value.
Comments
Post a Comment