Laravel 5.2 ajax代码不起作用

时间:2016-08-22 02:50:20

标签: ajax laravel-5.2

我开发了一个简单的ajax代码。根据这个图像点击+数量增加和点击时 - 数量减少 enter image description here

在线编码_help.blade.php

<head>
      <title>Ajax Example</title>
      <meta name="csrf-token" content="{{ csrf_token() }}">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<script>


           $(document).ready(function(){
              $('.cart_quantity_up, .cart_quantity_down').on('click', function(e) {
                       e.preventDefault();

    var increment = $(this).data('increase');
    var itemId = $('.cart_quantity_input').val();
                    $.ajax({
                          type:'post',
                          url:'getmsg',
                         dataType:'json', 
                         data:{
                          'item_id': itemId,
                          'increase': increment

                         },
                       success:function(data){
                     $("cart_quantity_input").val(data.qty);
                                     }   }); 
                                        });
               });
     </script>

</head>


<body>


 <div class="cart_quantity_button">
    <a class="cart_quantity_up" href="javascript:void(0)" data-increase="1" > + </a>
    <input class="cart_quantity_input" type="text" name="quantity" value="public function ajaxl(Request $request)
   {
         if ($request->ajax()) {
             $id = $request->item_id;

           if ($request->increase) {
                 //$cart->increment('qty');
            $qty = 2;

             } else {
                 //$cart->decrement('qty');
              $qty = 0;
             }
              //echo"hello ajaxlara:";


      return response()->json(array('qty'=> $id), 200);
                                }
    }
" autocomplete="off" size="2">
    <a class="cart_quantity_down" href="javascript:void(0)" data-increase="0" > - </a>
</div>

    </body>
<footer>

<script type="text/javascript">
       var csrf_token   =   $('meta[name="csrf-token"]').attr('content');
           $.ajaxSetup({
             headers: {"X-CSRF-TOKEN": csrf_token}
                       });
     </script>
</script>
</footer>

控制器功能中的代码

Route::get('/ajax',function(){return view('online_help');});
Route::post('/getmsg','Hello@ajaxl');

这个routes.php

DocFooter

当我点击+或 - 时,值不会发生变化而且没有错误。 请任何人帮助我

1 个答案:

答案 0 :(得分:1)

问题非常小。

这一行:

$("cart_quantity_input").val(data.qty);
只是你忘了添加。在cart_quantity_input之前 所以改成它是:     $(&#34; .cart_quantity_input&#34)。VAL(data.qty);