标签云

微信群

扫码加入我们

WeChat QR Code


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

2018年09月24日56分01秒

Added the portion of code to the question.

2018年09月24日56分01秒

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年09月25日56分01秒

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年09月24日56分01秒

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年09月24日56分01秒

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

2018年09月25日56分01秒

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

2018年09月24日56分01秒

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

2018年09月25日56分01秒

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年09月24日56分01秒

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年09月24日56分01秒

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

2018年09月24日56分01秒

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年09月24日56分01秒

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年09月24日56分01秒

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年09月24日56分01秒

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年09月24日56分01秒

OP uses JSON, not JSONP.

2018年09月25日56分01秒

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

2018年09月24日56分01秒

It worked for me, thanks

2018年09月25日56分01秒

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

2018年09月25日56分01秒

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年09月24日56分01秒

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

2018年09月25日56分01秒

What are the double $$ there?

2018年09月25日56分01秒

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

2018年09月24日56分01秒

bear The double dollar selector is defined in MooTools.

2018年09月25日56分01秒

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年09月24日56分01秒

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年09月24日56分01秒

OP uses JSON, not JSONP.

2018年09月25日56分01秒

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

2018年09月24日56分01秒

This question is about "Unexpected token :".

2018年09月25日56分01秒

OP asked about "Unexpected token :".

2018年09月24日56分01秒