I am trying to make a call into this API and return a json object. I want to return an array of the top answerers for the tag that I pass in. I'm having trouble building the string in my $.ajax call.
Link to API - - > http://api.stackexchange.com/docs/top-answerers-on-tags
This code works:
console.log(
$.ajax({
url: "http://api.stackexchange.com/2.2/tags/jquery/top-answerers/all_time?site=stackoverflow",
dataType: "jsonp",
type: "GET",
}));
But this code does not work. It throws an error in the console "Uncaught SyntaxError: Invalid or unexpected token"
var getTopAnswerers = function(tag) {
// tag is a string passed in via the html form
// the parameters we need to pass in our request to StackOverflow's API
var request = {
tag: tag,
period: 'all_time',
site: 'stackoverflow',
};
$.ajax({
url: "http://api.stackexchange.com/2.2/tags{tag}/top-answerers/",
data: request,
dataType: "json",
type: "GET",
})
So I tried hardcoding the tag value, still no luck. The API returns a list of tag objects like so that appears to be a list of question tags. http://api.stackexchange.com/2.2/tags?tag=jquery&period=all_time&site=stackoverflow.
var getTopAnswerers = function() {
var request = {
tag: 'jquery',
period: 'all_time',
site: 'stackoverflow',
};
$.ajax({
url: "http://api.stackexchange.com/2.2/tags",
data: request,
dataType: "json",
type: "GET",
})
};
I tried switching the dataType between json and jsonp as well.
TL;DR: Please help me build the proper string for $.ajax to interface with the API.
EDIT: It was pointed out I had some errant commas. Fixed the commas still does not build the string.
var getTopAnswerers = function() {
var request = {
tag: 'jquery',
period: 'all_time',
site: 'stackoverflow'
};
$.ajax({
url: "http://api.stackexchange.com/2.2/tags",
data: request,
dataType: "json",
type: "GET"
})
};