So I've been trying to create a live search bar for the project i'm currently working on. I have been following this guide: https://www.youtube.com/watch?v=y9WKRlJ1JGM
But is does not work for me, after a lot of testing I can safely say that the AJAX request is being sent to the correct controller, and that the necessary database queries are being executed. But Nothing is being sent back to the page. I have been on this for over a full day and can't seem to find any articles with a similar Issue. I am also getting no error messages whatsoever, it just doesn't do anything.
Here is some of my code:
Javascript
$('#searchbox').on('keyup', function(){
$value = $(this).val();
//alert($value);
$.ajax({
type: 'get',
url: 'livesearch',
data: {'search':$value},
succes:function(data){
alert(data);
}
});
})
Route:
Route::get('livesearch','AjaxController@livesearch');
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
class AjaxController extends Controller {
public function livesearch(Request $request) {
if ($request->ajax())
{
$output = '';
$searchresults = User::where('firstname', 'LIKE', '%' . $request->search . '%')->orWhere('lastname', 'LIKE', '%' . $request->search . '%')->get();
//print_r($searchresults);
if($searchresults)
{
foreach ($searchresults as $key => $searchresult) {
$output .= $searchresult->firstname .
' ' .
$searchresult->lastname .
' | ' .
$searchresult->company .
'<br>';
}
//return $output;
return Response($output);
//return Response::json(['data' => $output]);
}
}
}
}
Please ignore the commented code, I used it for testing where my code was failing. And as I said: everything seems to work up to the point the controller has to send the response back.
I really hope some of you guys can help me out. Thanks in advance!
via Robin Neefs