Uncaught SyntaxError: Unexpected token :
I am running an AJAX call in my MooTools script, this works fine in Firefox but in Chrome I am getting a Uncaught SyntaxError: Unexpected token :
error, I cannot determine why. Commenting out code to determine where the bad code is yields nothing, I am thinking it may be a problem with the JSON being returned. Checking in the console I see the JSON returned is this:
{"votes":47,"totalvotes":90}
I don't see any problems with it, why would this error occur?
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();
});
});
只是为可能有同样问题的人提供的一个参考 - 我只需要让我的服务器以应用程序/ json的形式发回JSON,并且默认的jQuery处理程序工作正常。
Seeing red errors
Uncaught SyntaxError: Unexpected token <
in your Chrome developer's console tab is often an indication of 301 Redirects that could be caused by having a strange rule in your .htaccess file.
What you're actually seeing is your browser's reaction to the unexpected top line <!DOCTYPE html>
from the server.
This has just happened to me, and the reason was none of the reasons above. I was using the jQuery command getJSON and adding callback=?
to use JSONP (as I needed to go cross-domain), and returning the JSON code {"foo":"bar"}
and getting the error.
This is because I should have included the callback data, something like jQuery17209314005577471107_1335958194322({"foo":"bar"})
Here is the PHP code I used to achieve this, which degrades if JSON (without a callback) is used:
$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;
}
Hopefully that will help someone in the future.
链接地址: http://www.djcxy.com/p/1246.html