简体   繁体   English

在Laravel 4中使用jquery Ajax

[英]Using jquery Ajax in Laravel 4

I am newbie to ajax jquery and trying to understand the code and trying to implement it. 我是ajax jquery的新手,尝试理解代码并尝试实现它。 I would let you know what exactly i want to do and what i am doing. 我会让你知道我到底想做什么以及我在做什么。

I have a search box where i input the "sku" and i get the tables and the information of that particular sku. 我有一个搜索框,在其中输入“ sku”,然后获取该特定sku的表格和信息。

I have this in my routes.php 我的routes.php中有这个

Route::get('bestsellers', array('as'=>'bestsellers', 'uses' =>'SalesFlatOrderItemsController@index'));

In my controllers i have 在我的控制器中

class SalesFlatOrderItemsController extends \BaseController {
$sku_query = Input::get('sku');

        if($sku_query){
        $orders = SalesFlatOrder::join('sales_flat_order_item as i','sales_flat_order.entity_id','=','i.order_id')
                         ->select((array(DB::Raw('DATE(i.created_at) as days'), DB::Raw('sum(i.qty_ordered) AS qty_ordered'), DB::Raw('sum(i.row_total) AS row_total'),'i.item_id', 'i.name','i.sku')))
                         ->where('i.sku','=',$sku_query)
                         ->groupBy('i.sku')
                         ->orderBy('qty_ordered','Desc')
                         ->paginate(10);

    }

    return View::make('sales_flat_order_items.bestsellers')->with('orders', $orders);
}

And In bestsellers.blade.php , i have bestsellers.blade.php ,我有

<input type="text" id="sku" placeholder="Search the sku..." name="sku">
<input type="hidden" id="search_sku" name="search_sku" value="">

<button type="button" id="searchSubmit" class="btn btn-info">Search</button><div class="spin-area" id="spin-area">
 <thead>
       <tr class="odd gradeX"> 
        <th>Sku</th>
      <th>Product Name</th>
      <th>Items Ordered</th>
      <th>Total</th>

        </thead>

  @foreach ($orders as $item )

   <tr class="odd gradeX"> 
    <td><a href="{{ URL::action('SalesFlatOrderItemsController@performance', $item->sku) }}">{{ $item->sku  }}</a></td>
    <td>{{ $item->name }}</td>
    <td>{{ round( $item->qty_ordered,2) }}</td>
    <td>{{ round( $item->row_total,2) }}</td>
  </tr>
 @endforeach
</table>
</div>
</div>
</div>
</div>

This is for the input sku should be entered and ajax should help to get the information of sku on the same page. 这是为了输入sku,而ajax应该有助于在同一页面上获取sku的信息。 So ajax is as below 所以ajax如下

<script>
$(document).ready(function(){

    $('#searchSubmit').on('click',function(){
      var data ="sku="+$('#sku').val();

      $.ajax({
        type:"GET",
        data:data,
        url:"/bestsellers",
        dataType:"JSON",
        success:function(data){
          alert('success');
        }

      })
    });
  });

</script>

Can somebody let me know what is going wrong with my code, Before this i have used traditional way of post and get request, it works, but not ajax call. 有人可以让我知道我的代码出了什么问题,在此之前,我使用了传统的发布和获取请求的方式,它可以工作,但不能使用ajax调用。
Please help. 请帮忙。
Thanks. 谢谢。

try this 尝试这个

$(document).on('click','#searchSubmit',function(){
    var data ="sku="+$('#sku').val();
    $.ajax({
        type:"GET",
        data:data,
        url:"{{URL::to('/bestsellers')}}",
        dataType:"JSON",
        success:function(data){
            alert('success');
            // data variable will have the data returned by the server. use it to show the response
        }
    })
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM