2017-07-23 4 views
-1

Dies ist die Webseite, wo ich org zeigen möchte. Graph. DataArray-Variablenspeicher [[{v:"John",f:"junior Employee"}, ,"Junior Employee"]] Wert. und ich habe Daten durch Ajax ist ["[{v:"John",f:"Junior Employee"}, ,"Junior Employee"]"].Wie korrigiere ich diesen Code, um Google Org anzuzeigen? Diagramm richtig?

Ich habe diesen Fehler:

SyntaxError: Unexpected token v in JSON at position 3 

Code:

<html> 
     <head> 
      <title>Case Graphs</title> 
      <script src="./js/my/jquery-2.1.1.min.js"></script> 
      <!--<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> --> 
      <script type="text/javascript" src="loader.js"></script> 
      <script type="text/javascript"> 

     $("document").ready(function(){ 
     $("#getGraph").click(function(){ 

     $.ajax({ 
     url:"./DataPHP3.php", 
     method:"POST", 
     dataType:'json', 
     success:function(data){ 
     console.log(data); 
     var length=data.length; 

     google.charts.load('current', {packages:["orgchart"]}); 
      google.charts.setOnLoadCallback(drawChart); 

      var dataArray=[]; 

     for(var k=0;k<length;k++) 
     { 
     if(k==0) 
     { 
     dataArray+="["+data[k]; 
     } 
     else 
     { 
     dataArray+=","+data[k]; 
     } 
     } 
     dataArray+="]"; 

     function drawChart() { 


       console.log(dataArray); 
       var data = new google.visualization.DataTable(); 
       data.addColumn('string', 'Name'); 
       data.addColumn('string', 'Manager'); 
       data.addColumn('string', 'ToolTip'); 

       // For each orgchart box, provide the name, manager, and tooltip to show. 
       data.addRows(
      $.parseJSON(dataArray) 
      // [[{v:"John",f:"junior Employee"}, ,"Junior Employee"]] 

       ); 

       // Create the chart. 
       var chart = new google.visualization.OrgChart(document.getElementById('chart_div')); 
       // Draw the chart, setting the allowHtml option to true for the tooltips. 
       chart.draw(data, {allowHtml:true}); 
       } 

     }, 
     }); 


     }); 
     }); 
      </script>`` 
     </head> 
     <body> 
     <input type="button" id="getGraph" value="Show Graph"> 
      <div id="chart_div" style="width: 900px; height: 400px"></div> 
     </body> 
     </html> 

This is webpage,where i want to show org. graph. dataArray variable store [[{v:"John",f:"junior Employee"}, ,"Junior Employee"]] 
value. and i got data through ajax is ["[{v:"John",f:"Junior Employee"}, ,"Junior Employee"]"]. 
I GOT THIS ERROR: 
SyntaxError: Unexpected token v in JSON at position 3 
+0

Verwendung dieser Methode data.setCell() -Daten in Tabelle zu schreiben, anstatt gesamte Format des Kopierens !!! –

+0

Formatierung verbessert. – Florian

Antwort

0

Notwendigkeit, die Objektschlüssel in Anführungszeichen als auch ("v":) ...

[[{"v":"John", "f":"junior Employee"}, null, "Junior Employee"]] 

siehe folgende einzuwickeln Arbeitsschnipsel ...

google.charts.load('current', { 
 
    callback: drawChart, 
 
    packages: ['orgchart'] 
 
}); 
 

 
function drawChart() { 
 
    var data = new google.visualization.DataTable(); 
 
    data.addColumn('string', 'Name'); 
 
    data.addColumn('string', 'Manager'); 
 
    data.addColumn('string', 'ToolTip'); 
 

 
    data.addRows(
 
    $.parseJSON('[[{"v":"John", "f":"junior Employee"}, null, "Junior Employee"]]') 
 
); 
 

 
    var chart = new google.visualization.OrgChart(document.getElementById('chart')); 
 
    chart.draw(data, { 
 
    allowHtml: true 
 
    }); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://www.gstatic.com/charts/loader.js"></script> 
 
<div id="chart"></div>

+0

sieht aus, als hätten Sie eine Lösung gefunden, aber Sie möchten vielleicht wissen, warum der Fehler aufgetreten ist ... – WhiteHat

Verwandte Themen