从MySQL数据库接收数据而无需重新加载页面

时间:2018-11-27 22:56:13

标签: php mysql ajax laravel

我正在尝试从数据库中检索数据,但是当地址栏中出现get参数时,页面上没有任何更改,因此我必须刷新页面以接收数据,而不是无需刷新/重新加载即可接收它。

路线:

Route::get('writers/{orders?}/{number?}', ['as'=>'writers','uses'=> 'HomeController@writers']);

控制器:

 public function writers($order='all',$num=10){
    $dm = new DataModel();
    $orders = $dm->getCertainWriters($num);
    $this->certainOrders =$orders;

    return view('writers')->with(array('title'=>'Writers','data'=>$this->certainOrders,));

}

方法:

  public function getCertainWriters($orders = 'all'){
    $data = DB::select("SELECT * FROM `writers` WHERE `completed_orders` > '$orders'  ");

    return $data;
}

AJAX:

 $("#ajax-orders").change(function(e) {
    var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
    var form = $(this);
    var url = form.attr('action');

    $.ajax({
        type: "GET",
        url: url,
          dataType:"html",
        headers: {       
            'X_CSRF_TOKEN':CSRF_TOKEN,
            'Content-Type':'application/json'
        },
        data: form.serialize(), // serializes the form's elements.
        success: function(data)
        {
           $(form).submit(e);
           var orders =  $('select').val();
             window.history.pushState("writer", "orders", "/writers/orders/"+orders);

        }

    });

     e.preventDefault(); // avoid to execute the actual submit of the form.
});

0 个答案:

没有答案