JQuery take url with variables from link and send it via Ajax

I can add the product to the cart via GET at this link:

<div id="add"><a id="add-link" href="http://localhost/catalog/cart?product_id=8&boutique_id[]=36&func=Module::Cart->add">Add to Cart</a></div>

I want to use jQuery Ajax to stay on the same page (if JS is enabled). I skipped the following, but of course it did not work. Can someone please see what is wrong? Thanks.

<script type="text/javascript">
$(document).ready(function(){

$('a#add-link').bind("click", function(event) {
event.preventDefault();
var url = $(this).attr("href");
    alert("Now I want to call this page: " + url);
$.ajax({
  type: "GET",
  url: "url="+url, 
  success: function() {
    alert("Product added to cart");
  }
});
});

});
</script>
+3
source share
4 answers
var url = $ (this) .attr ("href");
alert ("Now I want to call this page:" + url);
$ .get (url, function (resp) {
    alert ("Product added to cart");
});
+7
source

You do not need "url =", just use

url: url, 
+1
source
$(document).ready(function(){
  $('a#add-link').bind("click", function(event) {
    event.preventDefault();
    $.get($(this).attr('href'), function(data, status) {
      alert("Product added to cart");
    });
});
+1

, "url =" +

, URL- .

0

Source: https://habr.com/ru/post/1718493/


All Articles