1

我最近遇到了一个问题。我有一个脚本,可以通过 ajax 帖子将商品添加到我的 Opencart 的购物车中,并将结果添加到 div 中。但是,我似乎得到的响应与设置的响应不同

"Success: You have added <a href="%s">%s</a> to your <a href="%s">shopping cart</a>!" 

这是我在 div 中得到的响应:

{"success":"Success: You have added
<a href="\"http:\/\/mystore.org\/store\/index.php?route=product\/product&product_id=50\"">
Apple<\/a> to your</a><a href="\"http:\/\/mystore.org\/store\/index.php?route=checkout\/cart\"">
shopping cart<\/a>!","total":"3 item(s) - $20.99"}
</a>

这是我的脚本:

$(document).ready(function(){
$("#addform").submit(function() {
    $.post($("#addform").attr("action"), $("#addform").serialize(), function(data){
    $("#result").empty().slideDown("slow").append(data);
    });
    return false;
});
});

我只是一个初学者,所以如果可能,请提供详细信息。

非常感谢

4

1 回答 1

2

您正在收到一个 json 响应,以获取您需要的成功消息 -

$("#addform").submit(function() {
    $.post($("#addform").attr("action"), $("#addform").serialize(), function(data){

    // parse json response
    data = $.parseJSON(data);  

    // get success message with data.success and append it to results
    $("#result").empty().slideDown("slow").append(data.success);
    });
    return false;
});
于 2013-06-20T19:26:24.377 回答