0

I am trying to use the iTunes search API and the fetch api.

When I try the following I get a response the in the console.

  fetch('https://api.github.com/users/bootstrap').then(function(response) {
    return response.json();
  }).then(function(j) {
    console.log(j);
  });

However when I try

  fetch('https://itunes.apple.com/search?term=jack+johnson',{mode: 'no-cors'}).then(function(response) {
    return response.json();
  }).then(function(a) {
    console.log(a);
  });

I get an error Uncaught (in promise) SyntaxError: Unexpected end of input(…)

Is it possible to grab the iTunes API data in this way?

Thanks, Dave

davidjh
  • 387
  • 1
  • 7
  • 13
  • Possible duplicate of [What is an opaque request, and what it serves for?](http://stackoverflow.com/questions/36292537/what-is-an-opaque-request-and-what-it-serves-for) – Marco Castelluccio Jul 12 '16 at 13:33

1 Answers1

0

From MDN (https://developer.mozilla.org/en-US/docs/Web/API/Request/mode):

no-cors — Prevents the method from being anything other than HEAD, GET or POST. If any ServiceWorkers intercept these requests, they may not add or override any headers except for these. In addition, JavaScript may not access any properties of the resulting Response. This ensures that ServiceWorkers do not affect the semantics of the Web and prevents security and privacy issues arising from leaking data across domains.

That's why you get empty response in JS and the JSON error when parsing the empty string JSON.parse("").

Petr Odut
  • 1,667
  • 14
  • 7