jQuery-POST,成功回调未执行 [英] Jquery - POST, success callback is not getting executed

查看:160
本文介绍了jQuery-POST,成功回调未执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下Jquery代码将商品添加到购物车.我的意图是在Ajax调用成功后更改添加到购物车"按钮.但是它没有发生.代码如下-

I am using the below Jquery code to add items to cart. My intention is to change the "Add to cart" button once the Ajax call is success. However its not happening. Code is below -

  jQuery.post('/cart/add.js', {
              quantity: 1,
              id: variant_id
  }, function(data){
    var myelem = document.getElementById('red') ;
    if (myelem != null){
      document.getElementById('red').id = 'normal';
    }
    $("span").removeClass("hidden-count");
    $(".cart-count").text('1');
    $("#AddToCartText").text("Added");
    $("#AddToCart").css("color","Green");

  });

当前,代码将商品添加到购物车中,但是我没有看到预期的元素操作. 操纵元素的jquery是正确的,因为当我从成功回调中删除它,并将其放在AJAX调用之后(不依赖于AJAX调用)之后,在这种情况下,元素就得到了操纵.

Currently, the code is adding the item to cart but I am not seeing the intended element manipulations. The jquery to manipulate the elements is correct, because when I remove it from the success callback, and put it just after the AJAX call (without any dependency on AJAX call), in that case the element is getting manipulated.

  1. 我已提及 http://api.jquery.com/jquery.post/验证语法.
  2. 我已经在开发人员工具(Chrome)中选中了控制台",那里没有错误消息.
  1. I have referred to http://api.jquery.com/jquery.post/ to verify the syntax.
  2. I have checked "Console" in Developer tools (Chrome), there are no error messages there.

请让我知道我在哪里犯错.

Please let me know, where I am making mistake.

推荐答案

这在我的购物车中有效.注意第四个参数.该ID是变体ID:

This works in my cart. Notice the 4th parameter. The id is the variant id:

jQuery.post('/cart/add.js', {id:6028354753}, function(data){
  console.log(data);
},'json');

这篇关于jQuery-POST,成功回调未执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆