我在我的 blade 文件 show.blade.php 中有一个 AJAX 搜索,我在该显示方法中显示特定用户 网址: http://127.0.0.1:8000/client/1
这是我的路线
Route::resource('client', 'ClientController',
['as' => 'encoder']);
它是一个资源 Controller ,包含用于显示特定客户端的 show 方法。
这是我在我的 Controller
中所做的public function show($id, Request $request)
{
$client = $this->clientRepository->with(['request' => function ($query) {
$query->orderBy('created_at', 'desc');
}])->findWithoutFail($id);
$keyword = $request->get('keyword');
if (!$keyword) {
$client_requests = AnalysisRequest::where('client_id', $client->id)
->orderBy('created_at', 'desc')->get();
} else {
$client_requests = AnalysisRequest::where('client_id', $client->id)
->OrWhere('id', 'LIKE', '%keyword%')
->OrWhere('sample_descrition', 'LIKE', '%keyword%')
->OrWhere('special_instruction', 'LIKE', '%keyword%')
->orderBy('created_at', 'desc')->get();
}
// dd($client_requests);
if (empty($client)) {
Flash::error('Client not found');
return redirect(route('encoder.client.index'));
}
echo view('encoder-dashboard.client.show', compact('client_requests'))
->with('client', $client)
->render();
}
我有一个 show 函数,它将显示一个特定的客户端,该 id 作为参数传递到 Controller 中,以便显示该特定的客户端。现在,我也有一个针对我想要搜索的特定客户的请求。
这就是我在 AJAX 中所做的。
$(document).ready(function(){
$('.searchbar').on('keyup', function(){
var text = $('#searchbar').val();
$.ajax({
dataType: "json",
type:"GET",
url: '{{ url('encoder/client') }}' + '/' + $('.id_search').val(),
data: {text: $('.searchbar').val()},
success: function(response) {
console.log(response);
}
});
});
});
在我的show.blade.php
<div class="row">
<div class="col-sm-6">
<div class="form-group" id="results">
<input type="hidden" id="client_id" class="id_search" name="client_id" value="{{ $client->id }}">
<input class="form-control searchbar" id="searchbar" name="searchbar" placeholder="Search...">
</div>
</div>
</div>
@include('encoder-dashboard.client.request')
最后是 request.blade.php
<!-- The timeline -->
@if (isset($client_requests) && count($client_requests) > 0)
@foreach($client_requests as $request)
<ul class="timeline timeline-inverse">
<!-- timeline time label -->
<li class="time-label">
<span class="bg-red">
{{ $request->created_at->format('M d, Y') }}
</span>
</li>
<!-- /.timeline-label -->
<!-- timeline item -->
<li>
<i class="fa fa-edit bg-blue"></i>
<div class="timeline-item">
<span class="time"><i class="fa fa-clock-o"></i> {{ $request->created_at->diffForHumans() }}</span>
<h3 class="timeline-header">Request Code: <a href="{!! route('encoder.analysis-request.show', $request->id) !!}">{{ $request->reference_no() }}</a>
@if ($request->rushable == 1)
<p style="color:red">This request is for RUSH!</p>
@else
@endif
</h3>
<div class="timeline-body">
Description: <b>{{ $request->sample_description }}</b>
<br>
Service Requested: <b>{{ $request->service->description }}</b>
<br>
Category Requested: <b>{{ $request->category->name }}</b>
<br>
Method Requested: <b>{{ $request->methodology->name }}</b>
<br>
Special Instruction: <b>{{ $request->special_instruction }}</b>
<br>
@if ($request->status == 'for_testing')
Status: <span class="label label-warning">Pending</span>
@elseif ($request->status == 'under_analyzation')
Status: <span class="label label-info">Under Analyzation</span>
@elseif ($request->status == 'finished')
Status: <span class="label label-success">Finished</span>
@endif
</div>
<div class="timeline-footer">
<a class="btn btn-primary btn-xs" href="{!! route('encoder.analysis-request.show', $request->id) !!}">Read more</a>
{{-- <a class="btn btn-danger btn-xs">Delete</a> --}}
</div>
</div>
</li>
</ul>
@endforeach
@endif
我做错了什么?我现在在这里没有收到任何错误。
我之前遇到了一个500 内部服务器错误,但由于 url 错误而设法修复了它。
GET http://127.0.0.1:8000/encoder/client/1?keyword=dsa 500 (Internal Server Error) send @ jquery.min.js:4 ajax @ jquery.min.js:4 (anonymous) @ 1:363 dispatch @ jquery.min.js:3 q.handle @ jquery.min.js:3
我在 app.blade.php 和脚本 ajax 设置 header 中的元数据中有我的 csrf token 。
我是否会在 Controller 中创建一个单独的方法,该方法具有相同的 GET 请求,但没有传递任何参数?并将 id 作为隐藏的请求输入传递以用于其他目的。
也许我应该POST 请求 而不是GET 请求? 但是怎么做呢?因为我还显示了将 id 作为参数传递的特定资源。
感谢有人能提供帮助。 提前致谢。
最佳答案
在你的 blade 文件中,head 部分:
<meta name="csrf-token" content="{{ csrf_token() }}">
您的 aJax 中缺少 csrf_token header 。替换为:
$.ajax({
dataType: "json",
type:"GET",
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url: '{{ url('encoder/client') }}' + '/' + $('.id_search').val(),
data: {text: $('.searchbar').val()},
success: function(response) {
console.log(response);
}
});
});
关于php - Laravel 5.5 AJAX 实时数据搜索不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51261694/