2012-01-17 67 views
3

当我试图从一个JSON调用返回一个£符号£符号的时候,我得到了镀铬的错误:未捕获的SyntaxError:意外的标记非法未捕获的SyntaxError:意外的标记非法返回使用JSON

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字符串吗? – 2012-01-17 09:05:44

+0

我已经编辑我的问题,以显示JSON字符串 – CallumVass 2012-01-17 09:13:11

+0

在哪里?我看不到在你的代码英镑符号。 – 2012-01-17 09:14:36

回答

2

你不需要eval()因为你已指定的数据类型为JSON(jQuery将做JSONifying你可以简单地做:

... 
success: function (data) { 
    price = data.price; 
    currency = data.currency; 
}, 
... 
相关问题