0

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"
})
};
  • URL's in attempts don't come close to matching what works – charlietfl Jul 04 '16 at 02:19
  • I listed the easy peasy 'what works' url so folks have an idea as to what I'm trying to build. – Defect Assassin Jul 04 '16 at 02:28
  • Then you need to use the api docs to get url params figured out yourself and then make your code build the correct url. A Rest client like Postman will save you time testing params and results – charlietfl Jul 04 '16 at 02:33
  • If I could of figured it out myself via the api docs I would not be here seeking kindly assistance. I figured out enough of the youtube API to get what I need, but the way this particular API is expecting the string to be built has me stumped. – Defect Assassin Jul 04 '16 at 02:40
  • So that's why you play in a rest client like postman so you can test instantly without having to load in browser. Then reverse engineer wht works there – charlietfl Jul 04 '16 at 02:41
  • Roger that. I will look into that tool, it sounds helpful. Thank you for the tip. – Defect Assassin Jul 04 '16 at 02:45

1 Answers1

3

You need to call a handler when the Deferred object is resolved

function getTopAnswerers(tag) {
  var request = {
    site: 'stackoverflow'
  };
  $.ajax({
    url: "http://api.stackexchange.com/2.2/tags/"+tag+"/top-answerers/all_time",
    data: request,
    dataType: "json",
    type: "GET"
  }).done(function(data) {

    console.log(data);

  });
}

getTopAnswerers('jquery');
getTopAnswerers('html');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
Kld
  • 6,970
  • 3
  • 37
  • 50
  • This snippet is getting the same bad data I am - a list of question tags. – Defect Assassin Jul 04 '16 at 02:26
  • @DefectAssassin can you provide the format of the result that you want . – Kld Jul 04 '16 at 02:29
  • @KId - I put an example of the URL in the 'code that works' section. Here it is: http://api.stackexchange.com/2.2/tags/jquery/top-answerers/all_time?site=stackoverflow Thank you. – Defect Assassin Jul 04 '16 at 02:31
  • @KId Ahh, so in the API doc when they say "/tags/{tag}/top-answerers/{period}", the curly brackets mean that particular piece of the string has to be passed in via some sort of variable explicitly, outside of the usual params. – Defect Assassin Jul 04 '16 at 02:48
  • Yes it is a REST API url and not url parameters – Kld Jul 04 '16 at 03:02