Zac
Zac

Reputation: 12856

How to handle this json response

I am having trouble figuring out how to handle a json response. I just need to take all of the html part and put it into a div. So I can see in console the request is successful and returns something like :

{"goto_section":"review","update_section":{"name":"review","html":" <div> A bunch of html</div>"}}

How do I take this response and put it into my div?

$('my-div').html(response);

Thanks for any help and if you know any good resources where I could learn more I would appreciate it.

Upvotes: 1

Views: 206

Answers (3)

VisioN
VisioN

Reputation: 145478

Use this code:

var html = response.update_section.html;
// or using brackets: response["update_section"]["html"];

$('my-div').html(html);

JSON is a JavaScript object. Here is the good reference to read about working with JavaScript objects:

Upvotes: 1

Jeffrey Zhao
Jeffrey Zhao

Reputation: 5023

First, use "JSON" as the data type in your jQuery request, and in your success callback:

$(...).html(data.update_section.html);

Upvotes: 0

Andreas Wong
Andreas Wong

Reputation: 60594

$('my-div').html(response.update_section.html);

Also make sure that if you are using $.post or $.get, set the parameter after the callback to be json, or if you are using $.ajax, set dataType:json

Upvotes: 2

Related Questions