I'm trying to do a simple autofill for a subreddit searchbar.
Here is my jQuery code when I attempt to use the provided GET search option:
$.getJSON('http://www.reddit.com/reddits/search.json?q=%27+'+query+'+%27&limit='+AUTOFILL_LIMIT, function(data){
var results = data.data.children;
console.log('Query: "'+query+'"');
for(var i = 0; i < results.length; i++)
{
console.log(' '+results[i].data.display_name);
}
});
Here is my jQuery code when I attempt to use the provided POST search option:
$.getJSON('http://api.reddit.com/api/subreddits_by_topic?query=%27+'+query+'+%27', function(data){
var results = data.data.children;
console.log('Query: "'+query+'"');
for(var i = 0; i < results.length; i++)
{
console.log(results[i].data.display_name);
}
});
What am I doing wrong? I keep getting an {error: 404}
.
Your url is incorrect. It should be http://api.reddit.com/api/subreddits_by_topic.json?query=
Also, if you're using the POST search option (which that URL is not, by the way) then getJSON
won't work, as it's using a GET request method. You'll have to use $.post
or $.ajax
(which I recommend), if you're using jQuery.