2

I got a php page with the following JSON

{"id":"1","name":"name1","books":[{"id":"1","title":"title1"},{"id":"2","title":"title2"}]}

I am trying to fetch this but i keep getting the following output:

name1 [object Object], [object Object]

I use the following javascript code for the fetch:

window.onload=Send;
function Send() {
    var buttonSend = document.getElementById("buttonSend");
    buttonSend.onclick=HandleClick;
}

function HandleClick() {
    var $id = document.getElementById("textAuthorID").value;
    const URL = 'http://192.168.33.22/opdracht3/list.php?id=';
    fetch(URL + $id)
        .then( function(response) {return response.json(); })
        .then( function(data){ writeOutput(data.name + " " + data.books.join(", ")); } )
        .catch( function(exception) {writeOutput( exception );});
}

function writeOutput(text) {
    var textNode = document.createTextNode(text + "\n");
    var output = document.getElementById("divOutput");
    output.appendChild(textNode);
}

Could anyone help me out to display the names of the books?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
joske123
  • 35
  • 3
  • `data.books.join(", "))`. `data.books` is an array of objects, not strings. So calling join gets you a string of each objects string representation, which is `[object Object]` – Mark May 06 '18 at 23:28

0 Answers0