2012-01-17 64 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; 
}, 
... 
相關問題