jQuery意外令牌错误

时间:2013-08-16 10:35:40

标签: javascript jquery json

我收到此错误:Uncaught SyntaxError: Unexpected Token

这是我的代码:

$(document).ready(function(){ 

$(".cartsummary .showcartsumm").click(function() {
  $(".cartsummary .cartitems").show();    
});
$(".cartsummary").hoverIntent(
function () {
 $(".cartsummary .cartitems").fadeIn();
}, 
function () {
   $(".cartsummary .cartitems").fadeOut();
}) 
nutrishowcart(0);
});

function nutrishowcart(slidedown)
{
    $.ajax({
            type: "POST",
            url: "/CUSTminicart.aspx" + "/" + "Render",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "text",
            success: function(msg) { 
                // msg2 = eval(msg);
                // Get rid of the copyright text that kills JSON
                index = msg.indexOf('<div align="center">');
                msg2 = msg.slice(0,index);
                //msg2 = msg;
                json = eval("(" + msg2 + ")");
                $("#cartwrapper").html(json.d);

                if (slidedown==1) {
                    $(".cartsummary .cartitems").fadeIn().delay(2000).fadeOut();
                }
            },
            error:function (xhr, ajaxOptions, thrownError){
            alert("Minicart issue");
            }       

         });

}

这是在jquery.min.js的最后一行

错误似乎与json = eval("(" + msg2 + ")");

有关

有任何想法如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

这不是有效的JSON。对于JSON对象,它应该是json = eval("{" + msg2 + "}");

答案 1 :(得分:0)

您发现的错误是正确的。

要制作JSON对象,您应该使用“{”而不是“(”

json = eval("{" + msg2 + "}");