简体   繁体   中英

how to get success on jquery ajax request?

im new to ajax but i dont undersend the error i have this code:

function text_ajax(){
  $('.purchase-btn').click(function() {
    var JSONObject= {
      "prod_name":          $('.soc-name span').text(),
      "prod_quantity":      $('.soc-amount span').text(), 
      "prod_price":         $('.soc-price span').text(),
      "prod_line_price":    $('.soc-total span').text(),
      "prod_shipment_price":$('.soc-shipping-fee span').text(),
      "prod_vat":           $('.soc-vat-fee span').text(),
      "prod_total_price":   $('.soc-total-sum').text(),
      };

    $.ajax({
        type: "POST",
        dataType: "json",
        url: "/soda/checkout/ajax_post",
        data: {myData: JSON.stringify(JSONObject)},
        success: function(){
            alert('Items added');
        },
        error: function(e){
            alert(e.message);
        }
    });

  });
}

and i get all the elements but stil getting an error can somone help?

First, I would move the click event handler to call the function it is contained within.

Secondly, as ᾠῗᵲᄐᶌ stated there is an uncessary comma and the data does not need to be stringifed.

$('body').on('click', '.purchase-btn', function() {   
    var JSONObject= {
      "prod_name":          $('.soc-name span').text(),
      "prod_quantity":      $('.soc-amount span').text(), 
      "prod_price":         $('.soc-price span').text(),
      "prod_line_price":    $('.soc-total span').text(),
      "prod_shipment_price":$('.soc-shipping-fee span').text(),
      "prod_vat":           $('.soc-vat-fee span').text(),
      "prod_total_price":   $('.soc-total-sum').text()
    };

    $.ajax({
        type: "POST",
        dataType: "json",
        url: "/soda/checkout/ajax_post",
        data: JSONObject,
        success: function(){
            alert('Items added');
        },
        error: function(e){
            alert(e.message);
        }
    });

});

Please post the actual error message if there is one after using this modified code.

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