Hi,
I’m trying to test a live search using JSON and Ajax on Plunker.
However the code doesn’t work!
Is anything wrong with my code or Plunker isn’t able to handle Ajax requests?
Here is my example
[URL=“http://plnkr.co/edit/ZkXMcRsHCuYg4axN5DL8?p=preview”]http://plnkr.co/edit/ZkXMcRsHCuYg4axN5DL8?p=preview[/URL]
Thanks
It works for me. Though it outputs data
since you used quotes. If you want to output the data that is fetched by the JSON request, you should remove the quotes.
On which line should I remove the quotes?
The line where you console.log
data
.
Thanks for your reply. But the code you are talking about is very old version of it.
Could you please check again?
The script.js should look like this:
$('#search').keyup(function() {
var searchField = $('#search').val();
var myExp = new RegExp(searchField, "i");
$.getJSON('data.json', function(data) {
var output = '<ul class="searchresults">';
$.each(data, function(key, val) {
if ((val.name.search(myExp) != -1) ||
(val.bio.search(myExp) != -1)) {
output += '<li>';
output += '<h2>'+ val.name +'</h2>';
// output += '<img src="images/'+ val.shortname +'_tn.jpg" alt="'+ val.name +'" />';
output += '<p>'+ val.bio +'</p>';
output += '</li>';
}
});
output += '</ul>';
$('#update').html(output);
}); //get JSON
});