如何使用ajax检索数据而不需要发布页面?Laravel 5


How to retrieve data using ajax and without going to post page? Laravel 5

我是使用ajax的新手。例如,在字段title被填充后,我想在数据库中搜索特定的数据,并根据该输入返回更多的字段。到目前为止,我只能通过按下get-data/post-data或提交按钮在/ajax/post页面中接收我的title数据。在填写title期间/之后,如何从Route::post接收title输入和数据?如果我删除了Form::modelForm::close(),我确实可以通过单击Post data按钮从Route::post中获得我的伪数据,而不需要页面刷新,但没有标题值。

我知道检查title字段需要一些jQuery/js,但我不知道如何将title字段实际带到我的route中进行一些数据库搜索并返回一些数据。

视图:

            {!! Form::model($project = new 'App'Project, ['url' => 'ajax/post', 'method' => 'post']) !!}
            <!-- pass through the CSRF (cross-site request forgery) token -->
            <meta name="csrf-token" content="<?php echo csrf_token() ?>" />
            <!-- some test buttons -->
            <button id="get">Get data</button>
            <button id="post">Post data</button>
            <div class="form-group padding-top-10">
              {!! Form::label('title', 'Title') !!}
              {!! Form::text('title', null, ['class' => 'form-control', 'placeholder' => 'Title']) !!}
            </div>
            {!! Form::submit('Submit Button', ['class' => 'btn btn-primary form-control']) !!}
            {!! Form::close() !!}

Ajax脚本:

<script>
    $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } });
    function onGetClick(event)
    {
        // we're not passing any data with the get route, though you can if you want
        $.get('/ajax/get', onSuccess);
    }
    function onPostClick(event)
    {
        // we're passing data with the post route, as this is more normal
        $.post('/ajax/post', {payload:'hello'}, onSuccess);
    }
    function onSuccess(data, status, xhr)
    {
        console.log(data, status, xhr);
        // JSON is deserialised into an object
        console.log(String(data.value).toUpperCase())
    }
    $('button#get').on('click', onGetClick);
    $('button#post').on('click', onPostClick);
</script>

在途中:

    Route::get('/ajax/view', ['as' => 'home', 'uses' => 'AjaxController@view']);
    Route::get('/ajax/get', function () {
        $data   = array('value' => 'some get');
        return  Response::json($data);
    });
    Route::post('/ajax/post', function () {
        $data   = array('value' => 'some data', 'input' => Request::input());
        return  Response::json($data);
    });

您需要的是实现jquery keypress函数。

你来了js:

$("input.title").keypress(function(){
    var title = $(this).val();
    // now do the ajax request and send in the title value
    $.get({
        url: 'url you want to send the request to',
        data: {"title": title}, 
        success: function(response){
           // here you can grab the response which would probably be 
           // the extra fields you want to generate and display it
        }
    });
});

就Laravel而言,除了返回json:之外,您几乎可以将其视为典型的请求

Route::get('/url-to-handle-request', function({
    // lets say what you need to populate is 
   //authors from the title and return them
   $title = Route::get('title'); // we are getting the value we passed in the ajax request
    $authors = Author::where('title' ,'=', $title)->get();
    return response()->json([
        'authors' => $authors->toArray();
    ]);
}));

现在我可能会使用控制器,而不仅仅是在路线内做所有事情,但我想你会得到基本的想法。