2017-08-23 6 views
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のですなぜ記述セクションが表示されない?

+0

スクリプトは機能しますか? 'JSON.parse'のようなエラーはありますか?それともあなたを悩ませている 'console.log'ですか? – Bergi

+0

'JSON.parse'は部分JSONでエラーをスローします。ネットワークパネルは何を言っていますか、サーバーはすべての期待値を送信しますか?私はコンソールが長すぎる文字列を切り取っていると想像することができます。 – Bergi

+0

このスクリプトではエラーは発生しませんが、JSON.parseではすべてのJSONを完全に読み取らないため、その値を使用して更新機能に渡すことはできません。 jsonlint.comを使ってJSONを検証し、それを承認しました –

答えて

0

あなたの質問は答えが含まれています

[ 
{ 
    "level": "1", 
    "number": "1", 
    "title": "blah blah blah" 
}, 

この行は、JSONの構文解析とは何の関係もありません:あなたはconsole.log(request2.responseText)を行うと、あなたは次のような出力を受け取り

言います。これは単にレスポンスに含まれるものを出力するだけです。サーバーは応答の説明データを送信していません。

関連する問題