buffalowings
buffalowings

Reputation: 25

I ger Uncaught syntaxError JSON API javascript

I'm building a search for image program using flickr API and im stucked at a certain error.

"Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 async function (async) getData @ index.js:19"

I can't get any data from the API. Can someone please explain why I get this error and how to fix it?

Here is JavaScript code

const api_key = "123456789ABCDEFGH"; 
let quantity = "5";
const userSearch = document.getElementById("search-field"); // input search

async function getData() {
  const URL = `https://www.flickr.com/services/rest/?method=flickr.photos.search&api_key=${api_key}&per_page=${quantity}&tags=${encodeURIComponent(
    userSearch.value
  )}`;
  let response = await fetch(URL, { method: "GET" });
  let data = await response.json();
  return await data;  // HERE is data error.

Upvotes: 0

Views: 73

Answers (3)

buffalowings
buffalowings

Reputation: 25

I solved it and will explain here if someone else need's it. Also add this: &nojsoncallback=1`

  const URL = `https://www.flickr.com/services/rest/?method=flickr.photos.search&api_key=${api_key}&per_page=${quantity}&tags=${encodeURIComponent(
    userSearch.value
  )}&format=json&nojsoncallback=1`;




Upvotes: 0

buffalowings
buffalowings

Reputation: 25

Does nnot work, I had it before and removed just to try if it worked better without. Now when i added again, same problem..

If you mean like this:

  const URL = `https://www.flickr.com/services/rest/?method=flickr.photos.search&api_key=${api_key}&per_page=${quantity}&tags=${encodeURIComponent(
    userSearch.value
  )}&format=json`;

Upvotes: 0

Ruslan Gataullin
Ruslan Gataullin

Reputation: 478

Add format=json param to your url. By default this endpoint returns results in xml format

Upvotes: 1

Related Questions