1

I make a request in Ajax at the click of the button passing the id of a product to a controller and it passes to the helper. In the helper, through this id of the product, I add it to the cart, but that does not matter much on that issue. In this helper, I create an array with data and give a return on it and then the controller returns me that array. When I give an alert in the array, specifying the position of the array, it shows me the value. I wanted to display this array value in HTML code (in case it would be in h1 that I put in append).

Ajax request:

        function addCartao(product_id){
            alert("entrou");
            alert(product_id);
            $j.ajax({
              type: "POST",
              url: "<?php echo Mage::getUrl('fol_carousel/ajax/teste') ?>",
              data: {
                product_id: product_id
              },
              dataType: 'json',
              cache : false,
              beforeSend: function () {
                alert("beforeSend");
              },
              success: function (retorno) {
                alert('success');
                alert(retorno['name']);
                $j('.item-custom').append('<h1 class="product-name">(
The exact position value of the array should be displayed here)</h1>');
              },
              complete: function () {
                alert("complete");
              },
              error: function (x,y,z) {
                alert("error");
                alert(x);
                alert(y);
                alert(z);
                window.location.reload();
                history.go(0);
                window.location.href=window.location.href;
              }
          });
        }

Controller code:

$product_id = $this->getRequest()->getParams();
$dados = Mage::helper('fol_carousel/cartao')->addCustom($product_id);
echo json_encode($dados);

Array I create in the helper:

$dados['name'] = 'Teste';
$dados['price'] = "2.99";
return $dados;
1

The solution I found to the problem was by changing the .append() function of the ajax request, concatenating with the position of the array in the necessary place for it to be displayed.

Ajax request code with change:

        function addCartao(product_id){
            alert("entrou");
            alert(product_id);
            $j.ajax({
              type: "POST",
              url: "<?php echo Mage::getUrl('fol_carousel/ajax/teste') ?>",
              data: {
                product_id: product_id
              },
              dataType: 'json',
              cache : false,
              beforeSend: function () {
                alert("beforeSend");
              },
              success: function (retorno) {
                alert('success');
                alert(retorno['name']);
                $j('.item-custom').append('<h1 class="product-name">' + retorno['name'] + '</h1>');
              },
              complete: function () {
                alert("complete");
              },
              error: function (x,y,z) {
                alert("error");
                alert(x);
                alert(y);
                alert(z);
                window.location.reload();
                history.go(0);
                window.location.href=window.location.href;
              }
          });
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.