0
これは問題のjavascript部分です。私はJSON.parseを使用しています。コンソールにログインすると、説明キー/値が表示されません。JSON.parse(request.responseText)はすべてのデータを返しません
var jsonOptions = JSON.parse(request2.responseText);
jsonGlobal = jsonOptions;
console.log(request2.responseText);
私のJSONファイルは、この
[{"level": "1","number": "1","title": "blah blah blah","description": "aaaa"},
{"level": "1","number": "1.1","title": "blah again","description": "aaa" }]
のように見えるこれは、これは上の任意の考えコンソール
[
{
"level": "1",
"number": "1",
"title": "blah blah blah"
},
に表示するものである
<script>
var dataList2 = document.getElementById('json-datalist');
var input2 = document.getElementById('ajax2');
var request2 = new XMLHttpRequest();
var jsonGlobal ;
request2.onreadystatechange = function (response) {
if (request2.readyState === 4) {
if (request2.status === 200) {
// Parse the JSON
var jsonOptions = JSON.parse(request2.responseText);
jsonGlobal = jsonOptions;
console.log(request2.responseText);
// console.log(jsonGlobal)
// Loop over the JSON array.
jsonOptions.forEach(function (item) {
// Create a new <option> element.
var option = document.createElement('option');
// Set the value using the item in the JSON array.
option.value = item.title;
option.setAttribute('data-number', item.number);
option.setAttribute('data-description',item.description); // add this line for description
//<--
// Add the <option> element to the <datalist>.
dataList2.appendChild(option);
});
// Update the placeholder text.
input2.placeholder = "Start Enterting a Title...";
} else {
// An error occured :(
input2.placeholder = "Couldn't load datalist options :(";
}
}
};
// Update the placeholder text.
input2.placeholder = "Loading options...";
// Set up and make the request2.
request2.open('GET', '/static/json/all-titles.json', true);
request2.send();
</script>
いっぱいjavascriptのですなぜ記述セクションが表示されない?
スクリプトは機能しますか? 'JSON.parse'のようなエラーはありますか?それともあなたを悩ませている 'console.log'ですか? – Bergi
'JSON.parse'は部分JSONでエラーをスローします。ネットワークパネルは何を言っていますか、サーバーはすべての期待値を送信しますか?私はコンソールが長すぎる文字列を切り取っていると想像することができます。 – Bergi
このスクリプトではエラーは発生しませんが、JSON.parseではすべてのJSONを完全に読み取らないため、その値を使用して更新機能に渡すことはできません。 jsonlint.comを使ってJSONを検証し、それを承認しました –