Reputation: 45
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?
Upvotes: 0
Views: 132
Reputation: 359
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.
Upvotes: 4