rob.m rob.m - 24 days ago 13
jQuery Question

getJSON not outputting the html data into a div

If I try to output that getJSON in

console.log(data)
is fine, i see it, yet it doesn't go into the div as I am expecting to.

Trying this:

$.getJSON("http://en.wikipedia.org/w/api.php?action=parse&format=json&callback=?",
{
page:"Football", prop:"text"
}, function(data) {
$(".modal-content").html(data);
});

<div class="modal-content"></div>


NOTE: console gives no error and div is empty

Answer

You have to access the text property of the returned object correctly. Just trying to set .html(.. by passing the returned object will not work, because jquerys .html() method expects a function or a string as the parameter.

According to wikis API:JSON version 2 you can add the following property to the options object passed to $.getJSON.

formatversion:2

Eliminate useless indirection, e.g. {"text":"..."} instead of {"text":{"*":"..."}} and {"key1":"value1","key2":"value2"} instead of [{"name":"key1","*":"value1"},{"name":"key2","*":"value2"}].

So here is the correct anwser.

var location = "https://en.wikipedia.org/w/api.php?action=parse&format=json&callback=?";

$.getJSON(location, {
  page:"Football", 
  prop:"text", 
  formatversion: 2
}, function(data) {
    $(".modal-content").html(data.parse.text);
    //console.log(data.parse.text)
});
Comments