I know this discussion has been closed but I was wondering if you could help me out.
I’ve be trying to follow the example but I’m getting a console error “Uncaught SyntaxError: Unexpected token < in JSON at position 0”.
When checking the network response I get from the input field I’m getting the full html site instead of the Json. I’m guessing that’s why I’m getting the error, but I can’t seem to detect where the problem is. I’m relatively new to kirby and json.
var searchInput = $('#input-search');
var searchResultsWrapper = $('#wrapper-search-results');
$(searchInput).on(‘keyup’, function(e) {
// I check if the length in the input is more than 3 characters
if(searchInput.val().length > 3) {
$.ajax({
// build the url
url: "/kirby3/search?q="+searchInput.val()+"",
context: jQuery('#wrapper-search-results')
})
.done(function(data) {
console.log('someting');
// convert the data to objects, console.log this to see
// how the object is build and which keys you can use
var results = JSON.parse(data);
// Flush the container
searchResultsWrapper.html('');
// loop trough the objects in results and display them
jQuery.each(results, function(index, object) {
var string = '<div class="content-box"><a href="'+object.url+'" class="full-link"></a><time class="content-box__datetime">'+object.content.date+'</time><h2 class="content-box__title">'+object.content.title+'</h2><p>'+object.content.intro+'</p></div>';
// Append the results to the container
searchResultsWrapper.append(string);
});
});
}
I think your pattern is wrong, “kirby3” is probably just the folder your Kirby project is in, so that should not be part of the pattern. Try this instead:
Another issue is that query parameters are not allowed in the route pattern. You have to leave that out and can then get it with get('q') in your action function.
Thank you all for your support!
I configured the routing as @texnixe pointed out and modified the jquery and it’s working!
The only thing I changed was the url variable to include only the pattern. Then in the ajax method I added the input value since I wasn’t getting the complete url.