意外标记ILLEGAL ..某处 [英] Unexpected token ILLEGAL .. somewhere

查看:136
本文介绍了意外标记ILLEGAL ..某处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

addToBasket = (id, qty) ->
    if $.cookie('basket')?
        # Basket exists
        basket = $.parseJSON($.cookie('basket'))
        basket.push( { 'id': id, 'qty': qty } )
        $.cookie('basket', JSON.stringify(basket))
    else
        # Basket doesn't exist
        alert 'Creating basket'
        basket = JSON.parse([{'id': id, 'qty': qty}])
        $.cookie('basket', JSON.stringify(basket))

我撕掉了我的头发;我不能得到(编译的等效)函数运行,总是得到非法令牌错误。

I'm tearing my hair out; I cannot get the (compiled equivalent) function to run, always getting the illegal token error. I've checked for rogue, invisible characters and there's nothing besides CR/LFs in there.

推荐答案

您正在呼叫 JSON.parse ,这显然是限定为语法错误,而不是一个普通的异常,由于浏览器实现它的方式。你基本上是这样做的:

You're calling JSON.parse on an array, which apparently qualifies as a syntax error instead of a normal exception due to the way browsers implement it. You're essentially doing this:

JSON.parse([{id: 123}].toString())

与以下相同:

JSON.parse('[object Object]')

,因此出现错误。

这篇关于意外标记ILLEGAL ..某处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆