1

I'm trying to do a very basic GET request to the Codewars API, but for some reason I'm getting an error saying Unexpected token :.

Here is my code:

var apiKey = 'iJUXzfpo96eUXXa-BzsT';
var baseURL = 'https://www.codewars.com/api/v1';
var user = 'GiacomoSorbi';

// Example GET user
$.ajax({
    type: 'GET',
    url: `${baseURL}/users/${user}?${apiKey}`,
    dataType: 'JSONP',
    success: function (data) {
        console.log(data);
    }
});

and here's a JSFiddle. Any help would be appreciated.

EDIT:

Here is a link to the response I get.

Saad
  • 49,729
  • 21
  • 73
  • 112
  • 2
    This is a typical error if the endpoint returns JSON instead of JSONP. Can you post the response? Does the server support JSONP at all? – Felix Kling Oct 22 '15 at 06:14
  • 2
    ^ and that's it, [**https://www.codewars.com/api/v1/users/GiacomoSorbi?iJUXzfpo96eUXXa-BzsT**](https://www.codewars.com/api/v1/users/GiacomoSorbi?iJUXzfpo96eUXXa-BzsT) returns regular JSON, not JSONP – adeneo Oct 22 '15 at 06:17
  • Hey Felix, I updated my post to include the response. I'm not sure what you mean by the server supporting JSONP, I'm new to AJAX and I wanted to try making a simple API call in JSFiddle. I guess there's other stuff I'm not aware of? Yes, the link returns JSON not JSONP, but if I do `dataType: json`, it causes some other issue saying `XMLHttpRequest cannot load https://www.codewars.com/api/v1/users/GiacomoSorbi. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fiddle.jshell.net' is therefore not allowed access.` – Saad Oct 22 '15 at 06:18
  • 4
    The [docs](http://dev.codewars.com/) don't seem to mention JSONP at all and CORS headers are not set either. You cannot access this API directly from browser JS. – Felix Kling Oct 22 '15 at 06:19
  • I guess the first step would then be to learn what JSONP is: https://en.wikipedia.org/wiki/JSONP . Basically it's a response format. If the server doesn't provide the response in that format, you cannot tell jQuery to expect it. – Felix Kling Oct 22 '15 at 06:19

0 Answers0