【问题标题】:How to show ajax response in a product grid in php?如何在 php 的产品网格中显示 ajax 响应?
【发布时间】:2018-02-25 07:22:35
【问题描述】:

我有一个产品网格,最初在网格中显示产品列表。当我使用 ajax 调用按类别搜索产品时,我只想在网格内显示该类别的产品。如何输出此响应?

产品网格:

     <div class="col-sm-9">    
           @foreach($products as $product)          
              <div class="col-md-4">
                 <div class="products-gallery">       
                    <h4>{{$product->name}}</h4>                              
                 </div>  
              </div> 
            @endforeach                                                 
        </div> 

这是我的 javascript 部分:

    jQuery('#searchId').on('change', function()
        {
          var category_id = jQuery(this).val() 
          searchProduct(category_id)    
        })

        function searchProduct(category_id)
              {  
                $.ajax({
                    method: 'get', 
                    url: "{{url('searchByCategory')}}", 
                    data: {'category_id' : category_id}, 
                    success: function(html){                   
                        $(".col-sm-9").html(html)            
                    },
                    error: function(jqXHR, textStatus, errorThrown) { 
                        console.log(JSON.stringify(jqXHR));
                        console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
                    }
                    });                        
              }

这是我的 ajax 响应:

[{…}]
0:

category_id:3
created_at:"2017-09-11 20:33:19"
id:4
name:"HT 400 Triplex "
updated_at:"2017-09-11 20:33:19"

【问题讨论】:

  • 而不是返回 json(在你的情况下你需要 parseJSON 以便你可以写入 HTML)你可以在你的 ajax 中返回 html 并像 $(".col-sm-9").load(html), 一样简单地加载它,但是类型您的请求将是GET
  • 我的ajax请求是GET请求
  • 在下面查看我的答案

标签: php jquery ajax laravel laravel-5


【解决方案1】:
$(document).on('submit', 'form#yourFormID', function(event) {
    event.preventDefault();

    $.ajax({
        url: '/route/url',
        type: 'GET',
        dataType: 'html',
        data: {firstValue: 'foo', secondValue: 'bar'},
        success: function(html){
           $(".col-sm-9").html(html)
           //OR $(".col-sm-9").load(/route/url);
        }
    });


});


//route
Route::get('/route/url', 'ControllerName@fetchDataAction');

//Controller
public function fetchDataAction(Request $request){

    $data = ModelName::where('foo', $request->get('firstValue'))->where('bar', $request->get('secondValue'));

    return view('result', ['products' => $data]);
}

//result.blade.php
@foreach($products as $product)          
          <div class="col-md-4">
             <div class="products-gallery">       
                <h4>{{$product->name}}</h4>                              
             </div>  
          </div> 
        @endforeach

这应该会得到你想要的结果。

【讨论】:

  • 我在控制台中检查了返回的数据,但注意到在网格中显示。
  • 在您的页面中,如果您有另一个 .col-sm-9 可能是原因。最好使用 ID div class="col-sm-9" id="dataGrid" 然后 $('#dataGrid').html(html) 试试这个
  • 我在该页面中有一个 .col-sm-9。我检查了 Id ,实际上没有任何结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多