标签云

微信群

扫码加入我们

WeChat QR Code

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();
  });
});


The JSON is fine. The problem is probably how you handle it. Showing the code will help.

2018年07月17日05分44秒

Added the portion of code to the question.

2018年07月17日05分44秒

There doesn't seem to be anything wrong with the syntax, the JS nor JSON. Posting a (not)working test-case on jsfiddle.net would help - including HTML.

2018年07月18日05分44秒

I'm currently tethering internet so my modem compresses the HTML source of the websites I browse, so I can't really make heads or tails out of the code. But, for starters put every JS code in external files - this always makes debugging easier - you'll know weather the error is caused by JS or something else.

1970年01月01日00分03秒

An "unexpected token" is likely some illegal character code. Such a code is likely not to show up when you print to console. Therefore, print out the string one character at a time or use a protocol analyzer or debugger etc. to see the actual bytes of the string.

2018年07月17日05分44秒

I think I am having the inverse of this problem. I'm requesting JSON from a third party API and they are returning application/javascript as the response and I am getting the same error you reported in this question. Not sure how to deal with it.

2018年07月17日05分44秒

A strange problem is - it works fine on the localhost but not on the sever. Any idea why?

2018年07月18日05分44秒

Hello. I have exact problem and I can't solve it, can someone tell me how you send JSON back to server

2018年07月17日05分44秒

-1 This answer if no help to anyone having the same problem

2018年07月18日05分44秒

Thanks I was sending application/javascript (as I misread this SO ) and was getting this error on simple valid JSON. Changing it to application/json resolved the error. I'm not using JSONP.

2018年07月17日05分44秒

I have no idea why you are talking about "Unexpected token <" when question is about Unexpected token :. Your answer is completely unrelated to OP's problem.

2018年07月17日05分44秒

And how do I fix this? I have a php redirect that is causing this in Chrome.

2018年07月17日05分44秒

To add to andy_magoon, in my case, I had a script tag that was supposed to serve up javascript, but because the request was redirected to an HTML page (its not important why it was redirected), which begins with <!DOCTYPE html>, which is not valid javascript, the browser returns the SyntaxError when it attempt to parse the HTML as JS.

2018年07月17日05分44秒

Thom The way to fix it is to make sure that the HTTP get returns the javascript file that you're looking for, and not unexpected HTML.

2018年07月17日05分44秒

I got exactly this error because, as it turns out, the file path is incorrect and it can't find the file specified in script tag.

2018年07月18日05分44秒

It helped me a lot. We should wrap the json data with jsoncallback data whenever we are using jsonp method. Also the jsonpcallback data should not start with numbers. When I tried with numbers, it didn't worked out. When I tried it with the same format shown in this answer, it worked...

2018年07月17日05分44秒

OP uses JSON, not JSONP.

2018年07月18日05分44秒

True enough, but the error is the same and the code will degrade gracefully and still work if JSON is being used.

2018年07月17日05分44秒

It worked for me, thanks

2018年07月18日05分44秒

This worked for me with AJAX, jQuery & JSONP. Thanks a lot!

2018年07月18日05分44秒

the difference between standard request and request.json is mostly in the headers, it adds this.headers.extend({'Accept': 'application/json', 'X-Request': 'JSON'}); - go figure

2018年07月17日05分44秒

Weird that that is what was causing the issue with this.

2018年07月18日05分44秒

What are the double $$ there?

2018年07月18日05分44秒

DimitarChristoff --any answer for falsarella about the $$?

2018年07月17日05分44秒

bear The double dollar selector is defined in MooTools.

2018年07月18日05分44秒

I did this. I was echoing out json somewhere inmy php file and was getting php[//json data] in my response but I wasn't able to parse it. Thanks for posting this so I could figure out my own mistake.

2018年07月17日05分44秒

OP problem is completely different. In your case you're trying to parse HTML as JSON, and in OP's case (s)he is trying to parse JSON as JavaScript.

2018年07月18日05分44秒

OP uses JSON, not JSONP.

2018年07月18日05分44秒

I recommend using console.log() instead of alert() for debugging.

2018年07月17日05分44秒

This question is about "Unexpected token :".

2018年07月18日05分44秒

OP asked about "Unexpected token :".

2018年07月17日05分44秒