I'm doing an ajax GET request
$(dataTableSearchInputButtonID).on('click', function () {
$.ajax({
type: "GET",
url: "myUrl.test/tools",
dataType: "html",
success: function (resp) {
console.log(resp);
},
});
});
However, in the response in the console, I'm only seeing the HTML upon clicking the Search button (to retrieve and display data)
public function getTriviaTags(Request $request){
try {
$userSearch = $request->get('userSearch');
$tagsList = $this->tagRepository->getTags($userSearch);
$response = [
'tagsList' => $tagsList,
'userSearch' => $userSearch,
'trivia' => 'tag'
];
return view('pages.tools.main', $response);
} catch (Exception $exception) {
parent::log($exception, self::class);
}
}
How can I make sure that the $response variable appears (console.log(resp);) in the ajax success() method upon inputing text on the input field and click Search?
I need to append userSearch to the URL (myUrl.test/tools?userSearch="countries") so that the correct data appears. When I do it manually, in the search bar, it works fine.
Am I on the right track of achieving this?
You should not return view, but JSON response:
public function getTriviaTags(Request $request){
try {
$userSearch = $request->get('userSearch');
$tagsList = $this->tagRepository->getTags($userSearch);
$response = [
'tagsList' => $tagsList,
'userSearch' => $userSearch,
'trivia' => 'tag'
];
return response()->json($response)
} catch (Exception $exception) {
parent::log($exception, self::class);
}
}
And then you can access response variables via console.log(resp.data)