Jquery 以JSON格式返回数据的最佳方法

Jquery 以JSON格式返回数据的最佳方法,jquery,html,json,woocommerce,Jquery,Html,Json,Woocommerce,大家好,我用ajax修改了我网站上的一个div 我有一个完美的函数,但是我有一个问题,就是如何获取返回的值 updatecart = function(qty, id_product) { var currentVal, data, id, item_hash, request; currentVal = void 0; id = void 0; data = void 0; item_hash = void 0; currentVal = par

大家好,我用ajax修改了我网站上的一个div

我有一个完美的函数,但是我有一个问题,就是如何获取返回的值

updatecart = function(qty, id_product) {
    var currentVal, data, id, item_hash, request;
    currentVal = void 0;
    id = void 0;
    data = void 0;
    item_hash = void 0;
    currentVal = parseFloat(qty);
    id = parseInt(id_product);
    request = $.ajax({
      url: ajax_object.ajax_url,
      method: 'POST',
      data: {
        quantity: currentVal,
        id_product: id,
        action: 'update_my_cart',
        dataType: "json"
      },
      success: function(html, data) {
        var array_fields, fields, pos, total_amount;
        pos = html.indexOf("[");
        fields = html.slice(pos + 1, -1);
        array_fields = fields.split(',');
        id = parseInt(array_fields[0].replace(/\"/g, ""));
        total_amount = (array_fields[1] + ',' + array_fields[2]).replace(/[\\"]/g, '');
        $('.total' + id + ' .woocommerce-Price-amount').replaceWith($(total_amount));
        $('.amount_cart .amount').replaceWith('.amount', html);
        console.log('update cart');
      },
      error: function(errorThrown) {
        console.log(errorThrown);
      }
    });   };
在本例中,返回html和数据

data=“成功”

html是

<div class="total_purchase">
  <div class="col-xs-12">
    <div class="subtotal_cart">
      <span class="item_purchase">
        Sub-total:      </span>
      <span class="amount_cart">
        <span class="woocommerce-Price-amount amount">15,40&nbsp;<span class="woocommerce-Price-currencySymbol">&euro;</span></span>      </span>
    </div>
  </div>

<div class="col-xs-12">
  <div class="wc-proceed-to-checkout">

<a href="http:../checkout/" class="checkout-button button alt wc-forward">
    Checkout</a>
  </div>
</div>

</div>
什么是最好的方式或以其他方式返回日期以获取我需要的信息


谢谢你的建议

服务器端您必须使用json_encode()返回日期

如果您想在HTML中显示日期,您必须在ajax成功中实现这一点

success: function(json) {
    $('.amount_cart .amount').html(json.mydate);
}

您没有显示您的
更新我的购物车()
ajax回调。尽管John的回答是您需要添加到回调中的内容。
json_encode(array( 'mydate' => $my_date ));
success: function(json) {
    $('.amount_cart .amount').html(json.mydate);
}