简体   繁体   中英

jQuery unexpected token error

I am getting this error: Uncaught SyntaxError: Unexpected Token

This is my code:

$(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");
            }       

         });

}

This is on the last line in jquery.min.js

The error seems to be with json = eval("(" + msg2 + ")");

Any ideas how to resolve this?

This isn't valid JSON. It should be json = eval("{" + msg2 + "}"); for a JSON object.

The error you spotted out is right.

To make JSON object you should use "{" instead of "("

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM