1

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?

1
  • 1
    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] Commented May 6, 2018 at 23:28

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.