Мой код.
$(document).ready(function(){
$('#search_str').on('change input',function(e){
console.log(e)
e.preventDefault();
var url = LANG_URL
if($(this).val().length > 0){
$.ajax({
url: "/ajax/live-search/" + $(this).val() ,
type: 'GET',
dataType:'json',
contentType: 'application/json; charset=utf-8',
data:{},
success: function(data) {
$('.search-similar').html(' ')
var location = $('#search_str').attr('data')
var search_val = $('#search_str').val()
var search_result
if((data.text && data.text.length > 0) && (data.category && data.category.length > 0)){
$('.search-similar').show()
for( var i = 0; i < data.text.length; i++) {
if(location){
$('.search-similar').append("<li class='search-similar-item'><a class='search-similar-item-link' href='" + url + "list/q-" + data.text[i] + "/location'><span class='search-similar-item-value'>" + search_val + "</span><span class='search-similar-item-text'>" + data.text[i] + "</span></a></li>");
search_result = data.text[i]
} else {
$('.search-similar').append("<li class='search-similar-item'><a class='search-similar-item-link' href='" + url + "list/q-" + data.text[i] + "'><span class='search-similar-item-value'>"+ search_val + "</span><span class='search-similar-item-text'>" + data.text[i] + "</span></a></li>");
search_result = data.text[i]
}
}
for( var i = 0; i < data.category.length; i++) {
var category = getUrl(data.category[0])
$('.search-similar').append(" <li class='search-similar-item'><a class='search-similar-item-link' href='" + url + category.key +"/q-" + search_val + "><span class='search-similar-item-value'>" + search_val + "</span><span class='search-similar-item-text'>" + search_result + " в рубрике" + category.index + "<span></a></li>");
}
} else {
$('.search-similar').hide()
}
function getUrl(o){
let index,key
for(keys in o) {
index = Object.keys(o[keys])
key = Object.values(o[keys])
}
return {
index,
key
}
}
}
});
} else {
$('.search-similar').hide()
}
});
});