Page 1 of 1

[SOLVED] Uncaught SyntaxError: Unexpected token

Posted: Sat Jan 10, 2015 4:49 pm
by BillyAB
Hi All,
I was wondering if someone could help me with this.
I have this JQuery code

Code: Select all

$.getJSON("http://webservice.fanart.tv/v3/movies/" + movieID + "?api_key=4e6175137822b938951bee8a411a251d&callback=?", function (json) {
console.log(json);
var thumbURL = json[0].moviethumb[0].url;
where_to_show_poster_selector.append('<img src="' + thumbURL + '" title="' + data.results[0].title + '" />');
})
When this is run, in my chrome console i get " Uncaught SyntaxError: Unexpected token"
See Image in Spoiler
Image
Thanks in Advanced
BillyAB

Re: Uncaught SyntaxError: Unexpected token

Posted: Sat Jan 10, 2015 6:24 pm
by Kode
Not 100% sure, it's possibly because the request is cross domain so it's expecting JSONP rather than JSON

*edit* I see in your request now &callback=? so it's defintitely expecting JSONP, the API doesn't currently support JSONP, a work around is to call the api on the server side in a script and request that local script in your JSON request.

Re: Uncaught SyntaxError: Unexpected token

Posted: Sun Jan 11, 2015 1:55 pm
by BillyAB
Hi Thanks, I thought that was the issue.
We needed to make the move to using PHP for the requests over JS so i'll share my function for anyone else having an issue.

Code: Select all

function GetMovieThumb($MovieID)
{
    $URL = "http://webservice.fanart.tv/v3/movies/".$MovieID."?api_key=4e6175137822b938951bee8a411a251d";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $URL);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    $FArt = curl_exec($ch);
    curl_close($ch);

    $FArt = json_decode($FArt, TRUE
    );
//var_dump($json);

    $moviethumb = $FArt["moviethumb"];

    $moviethumb = $moviethumb[0];
    $moviethumb = $moviethumb["url"];

    return $moviethumb;
}
Cheers Billy