获取api [对象对象]

时间:2018-05-06 23:21:00

标签: javascript json api fetch

我有一个包含以下JSON的php页面

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

我正在尝试获取此信息,但我不断获得以下输出:

name1 [object Object], [object Object]

我使用以下javascript代码进行获取:

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);
}

有人可以帮我出示这些书的名字吗?

0 个答案:

没有答案