Sto eseguendo una chiamata AJAX nel mio script MooTools, questo funziona bene in Firefox ma in Chrome sto ottenendo un Uncaught SyntaxError: Unexpected token :
error, non posso determinare perché. Commentando il codice per determinare dove è il codice cattivo non si ottiene nulla, sto pensando che potrebbe essere un problema con il JSON che viene restituito. Controllando nella console vedo che il JSON restituito è questo:
{"votes":47,"totalvotes":90}
Non vedo alcun problema, perché dovrebbe verificarsi questo errore?
vote.each(function(e){
e.set('send', {
onRequest : function(){
spinner.show();
},
onComplete : function(){
spinner.hide();
},
onSuccess : function(resp){
var j = JSON.decode(resp);
if (!j) return false;
var restaurant = e.getParent('.restaurant');
restaurant.getElements('.votes')[0].set('html', j.votes + " vote(s)");
$$('#restaurants .restaurant').pop().set('html', "Total Votes: " + j.totalvotes);
buildRestaurantGraphs();
}
});
e.addEvent('submit', function(e){
e.stop();
this.send();
});
});
Questo è appena successo a me, e la ragione non era nessuno dei motivi di cui sopra. Stavo usando il comando jQuery getJSON e aggiungendo callback=?
per usare JSONP (dato che avevo bisogno di andare cross-domain), e restituendo il codice JSON {"foo":"bar"}
e ottenendo l'errore.
Questo perché avrei dovuto includere i dati di callback, qualcosa come jQuery17209314005577471107_1335958194322({"foo":"bar"})
Ecco il codice PHP che ho usato per ottenere questo, che si degrada se si usa JSON (senza callback):
$ret['foo'] = "bar";
finish();
function finish() {
header("content-type:application/json");
if ($_GET['callback']) {
print $_GET['callback']."(";
}
print json_encode($GLOBALS['ret']);
if ($_GET['callback']) {
print ")";
}
exit;
}
Speriamo che questo possa aiutare qualcuno in futuro.
Ho appena risolto il problema. C'era qualcosa che causava problemi con una chiamata standard a Request, quindi questo è il codice che ho usato al suo posto:
vote.each(function(element){
element.addEvent('submit', function(e){
e.stop();
new Request.JSON({
url : e.target.action,
onRequest : function(){
spinner.show();
},
onComplete : function(){
spinner.hide();
},
onSuccess : function(resp){
var j = resp;
if (!j) return false;
var restaurant = element.getParent('.restaurant');
restaurant.getElements('.votes')[0].set('html', j.votes + " vote(s)");
$$('#restaurants .restaurant').pop().set('html', "Total Votes: " + j.totalvotes);
buildRestaurantGraphs();
}
}).send(this);
});
});
Se qualcuno sa perché l'oggetto Request standard mi stava dando problemi, mi piacerebbe saperlo.