从mysql数据库接收数据而不重新加载页面

62lalag4  于 2021-06-17  发布在  Mysql
关注(0)|答案(0)|浏览(226)

我试图从数据库中检索数据,但是 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.
});

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题