Okoroafor Chukwuemeka
Okoroafor Chukwuemeka

Reputation: 152

how to get append data from a json response from js to html

please I'm new to JS. I have an AJAX which returns a JSON and I'd like to append to html fields the result from this JSON response.

Here's my JSON response form chrome inspector:

Object {message: Object}
message
:
Object
created_at
:
"2017-06-16 23:12:33"
data
:
"{"subject":"New Message","message":"Admin sent a new message"}"
id
:
"d93e8aa4-c56f-4312-9d2a-f7609a67acdf"
notifiable_id
:
1
notifiable_type
:
"App\Models\Doctor"
read_at
:
null
type
:
"App\Notifications\AdminMessage"
updated_at
:
"2017-06-16 23:12:33"
__proto__
:
Object
__proto__
:
Object

I've been able to append the 'created at' field to my html by doing:

success:function(result){
console.log(result);                         $("#msg_date").html(result.message.created_at);              $("#msg_content").html(); 
 }

but I've tried my best to append the data[subject] and the data[message] but don't really know how to retrieve the file from JSON. when I do

$("#msg_content").html(result.message.data); 

I get the whole data dumped but don't know how to access the fields individually.

Upvotes: 1

Views: 1188

Answers (1)

Okoroafor Chukwuemeka
Okoroafor Chukwuemeka

Reputation: 152

i used

var content = JSON.parse(result.message.data);

then i accessed the message and subject content by:

`content.message`
 content.subject

and it worked for me. Thank you

Upvotes: 1

Related Questions