I have an online shop with a shopping cart. The cart, which is a <table>
, refreshes its content after adding an article to the cart.
I use jQuery's AJAX method which receives HTML <td><tr>
as a response from the called PHP script.
Firebug's console shows the correct response from the call.
As you can see in my code, I want to add HTML to the table. I can't get it to work.
Do I not understand the AJAX method? I want to add these <td><tr>
to the shopping cart table.
$.ajax({
url: 'php/addToShoppingCart.php',
type: 'POST',
dataType: 'html',
data: content,
complete: function(data) {
$('#shop section table tbody').append(data);
},
});
To retrieve that page using jQuery's AJAX function, you would simply use some javascript similar to the following. $. ajax({ url: 'test. html', dataType: 'html' });
Hello @kartik, What you need to do is pass a callback function to the somefunction as a parameter. This function will be called when the process is done working (ie, onComplete): somefunction: function(callback){ var result = ""; myAjax = new Ajax.
You can't as it's asynchronous. If you want to do anything with it, you need to do it in a callback. How? Because it's asynchronous, javascript will fire off the ajax request, then immediately move on to execute the next bit of code, and will probably do so before the ajax response has been received.
The $.ajax() function returns the XMLHttpRequest object that it creates. Normally jQuery handles the creation of this object internally, but a custom function for manufacturing one can be specified using the xhr option.
have tried it using .done()?
$.ajax({
url: 'php/addToShoppingCart.php',
type: 'POST',
dataType: 'html',
data: content,
}).done(function ( data ) {
$('#shop section table tbody').append(data);
});
You can use the success also
$.ajax({
url: 'php/addToShoppingCart.php',
type: 'POST',
dataType: 'html',
data: content,
success : function(data)
{$('#shop section table tbody').append(data);}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With