使用JSON返回英镑符号时出现未捕获语法错误:Unexpected token ILLEGAL。

3

当我尝试从JSON调用中返回一个英镑符号时,在chrome中会出现错误:Uncaught SyntaxError: Unexpected token ILLEGAL

var currency = "";
var price = "";


$.ajax({
  type: 'GET',
  url: '../JSONDeliveryPrice/',
  dataType: 'json',
  success: function (data) {
    price = eval(data.price);
    currency = eval(data.currency);
  },
  async: false
});
console.log(price);
console.log(currency);

货币应该等于“£”,但我得到了那个错误。我需要以某种方式编码/解码值吗?如果我仅返回价格,则价格输出正确。
编辑:
public virtual ActionResult JSONDeliveryPrice()
        {
            string currency = "£";
            decimal price = 123;            
            return Json(new { price = price, currency = currency }, JsonRequestBehavior.AllowGet);
        }

2
请问您能否展示一下您的JSON字符串? - ldiqual
我已经编辑了我的问题,以展示JSON字符串。 - CallumVass
在哪里?我在你的代码中看不到英镑符号。 - user647772
1
@BiffBaffBoff 你仍然没有展示给我们JSON字符串,你只是展示了生成它的代码,请让我们看到确切的JSON字符串,例如使用 fiddler,在提取数据时不要使用eval(),可以使用var result = $.parseJSON(data);,但绝对不能使用eval() - balexandre
嗨,谢谢,我通过不使用eval()解决了它。 - CallumVass
1个回答

2
您不需要使用eval(),因为您已经指定了数据类型为JSON(jQuery会自动将其转换为JSON格式)。您只需简单地执行以下操作即可:
...
success: function (data) {
    price = data.price;
    currency = data.currency;
},
...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接