Sintassi: Errore JSON.parse, quando si cerca di caricare i dati per Protovis
-
11-10-2019 - |
Domanda
Ciao sto imparando come lavorare con Protovis, fin qui tutto bene, ma ora sono incappato in un problema che non riesco a risolvere.
Il seguente è il codice. ( Ho l'ultima jquery caricata nei miei intestazioni )
<script type="text/javascript+protovis">
var dataURL = "http://eagereyes.org/media/2010/protovis-primer/earthquakes.json";
var JSONdata = $.ajax({ type: "GET", url: dataURL, async: false }).responseText;
var earthquakes = JSON.parse(JSONdata);
var width = 560;
var height = 245;
var barWidth = width/earthquakes.length;
var gap = 2;
new pv.Panel().width(width).height(height+5)
.add(pv.Bar)
.data(earthquakes)
.bottom(0)
.width(barWidth-gap)
.height(function(d) d.Magnitude * (height/9))
.left(function() this.index * barWidth)
.root.render();
Quando provo questo in Firefox ottengo questo avviso:
Syntax:Error JSON.parse
Ho convalidato il JSON su http://www.jsonlint.com/ già. Quindi il problema deve essere altrove.
Qualcuno sa che cosa sta succedendo qui?
Modifica
ho cercato di caricare gli stessi dati in app protoviewer: http://www.rioleo.org/protoviewer/ e funziona. Così deve essere il codice.
Soluzione
Hai provato un callback asincrona regolare invece di un approccio sincrono? Come:
var dataURL = "http://eagereyes.org/media/2010/protovis-primer/earthquakes.json";
$.ajax({
type: "GET",
url: dataURL,
success: function(response) {
var earthquakes = JSON.parse(JSONdata);
var width = 560;
var height = 245;
var barWidth = width/earthquakes.length;
var gap = 2;
new pv.Panel().width(width).height(height+5)
.add(pv.Bar)
.data(earthquakes)
.bottom(0)
.width(barWidth-gap)
.height(function(d) d.Magnitude * (height/9))
.left(function() this.index * barWidth)
.root.render();
}
});
Inoltre, è quel file JSON si trova sullo stesso server che la pagina rendendo gli spettacoli di richiesta nella barra degli indirizzi (esattamente http://eagereyes.org
)?
Infine, il JSON.parse () passaggio manuale non è necessaria. Se si aggiunge il parametro dataType: 'json'
, $ .ajax () si deserializzare automaticamente come JSON e utilizza JSON.parse (), se disponibili.
Altri suggerimenti
Aggiungi un ;
punto e virgola alla fine della vostra risposta ??p>
Quale browser stai usando? Alcuni browser non definiscono l'oggetto JSON
. È possibile scaricare uno script dal seguente URL che definirà l'oggetto JSON
se non esiste già.
https://github.com/douglascrockford/JSON-js
È possibile verificare se JSON
è definito come segue:
alert(JSON);
Aggiorna
OK prossima cosa che farei è controllare che la chiamata AJAX è in realtà tornando i dati corect. Modificare il codice per stampare la JSON restituito dalla chiamata AJAX.
var JSONdata = $.ajax({ type: "GET", url: dataURL, async: false }).responseText;
alert(JSONdata);
var earthquakes = JSON.parse(JSONdata);
$.ajax({
type: "POST",
url: "saveChangesInEditing.php",
data: idObject,
success: function(data){
dataObject = JSON.parse(data);
$("input[name = 'id']").val(dataObject.id);
$("input[name='full_name']").val(dataObject.full_name);
$("input[name='sport']").val(dataObject.sport);
$("input[name='idol']").val(dataObject.idol);
},
error: function(data){
alert("error!" + data);
}
});