mercredi 6 mai 2015

Symfony2 using two different ajax scripts(failed to load response data)

So I have two ajax scripts. One that increases the value and one that decreases. The first scripts, that increases the value works, however the second script which is almost identical does not work and I am getting Failed to load response data in devTools. I dont know why, but maybe you cant use two ajax scripts like that? Maybe I need to create a new file for each script? I really dont know...

Well this is my script file :

$(document).ready(function () {
    $(document).on('click', '.plus', function (e) {
    $this = $(this);

    $.ajax({
        type: 'POST',
        url: 'add/quantity',
        dataType: 'JSON',
        data: {product: $this.parent('.input-append').find('input').data('id'),quantity: $this.parent('.input-append').find('input').val()},
        success: function (data) {      
          if(data.success == false){
           alert('error')
          }else{

           }
        }
    });
});

    $(document).on('click', '.minus', function (e) {
    $this = $(this);

    $.ajax({
        type: 'POST',
        url: 'remove/quantity',
        dataType: 'JSON',
        data: {product: $this.parent('.input-append').find('input').data('id'),quantity: $this.parent('.input-append').find('input').val()},
        success: function (data) {
          if(data.success == false){
           alert('error')
          }
        }
    });
});
});

And my controller for both scripts:

public function addQuantityAction( Request $request ) {
    $response = new JsonResponse();
    $requestData = $request->request->all();
    $productid     = $requestData['product'];
    $quantity = $requestData['quantity'];
    /** logic*/
    $em = $this->getDoctrine()->getManager();
    $product = $em->getRepository('MpShopBundle:Product')->find($productid);
    $qtyAvailable = $product->getStock();
    $session = $this->getRequest()->getSession();
    $cart = $session->get('cart', array());
    if ( $qtyAvailable > $cart[ $productid ] ) {
        $cart[ $productid ] = $cart[ $productid ] + 1;
        $response->setData(array('success'=>true,'message'=>'Qunatity increased'));
         $session->set('cart', $cart);
    } else {
        $response->setData(array('success'=>false,'message'=>'Out of stock'));
    }
    return $response;
}

public function removeQuantityAction( Request $request ) {
    $response = new JsonResponse();
    $requestData = $request->request->all();
    $productid     = $requestData['product'];
    $quantity = $requestData['quantity'];
    /** logic*/
    $em = $this->getDoctrine()->getManager();
    $product = $em->getRepository('MpShopBundle:Product')->find($productid);
    $qtyAvailable = $product->getStock();
    $session = $this->getRequest()->getSession();
    $cart = $session->get('cart', array());
    if ( $cart[ $productid] > 1 ) {
        $cart[ $productid ] = $cart[ $productid ] - 1;
        $response->setData(array('success'=>true,'message'=>'Qunatity decreased'));
         $session->set('cart', $cart);
    } else {
        $response->setData(array('success'=>false,'message'=>'Cant be lower than 1'));
    }
    return $response;
}

Routing.yml:

add_quantity:
  pattern:  /add/quantity
  defaults: { _controller: MpShopBundle:Homepage:addQuantity }

remove_quantity:
  pattern:  /remove/quantity
  defaults: { _controller: MpShopBundle:Homepage:removeQuantity }

As you can see the code is practically identical. It should work, but im not getting any response for the second script.. why?

When im trying to get the response i get status:302 and it redirects me back to the same page... Anyone know why?

Aucun commentaire :

Enregistrer un commentaire