0

EDIT: I was able to solve this, thanks for you time

I am trying to make an autocomplete on a search form for youtube videos.

I got the url you can use from here: Youtube API search auto-complete

And I'm using this script (though I don't think it has much to do with the problem I'm having): https://goodies.pixabay.com/javascript/auto-complete/demo.html

code I'm using

  var xhr;
  new autoComplete({
      selector: '.search-box',
      source: function(term, response){
          try { xhr.abort(); } catch(e){}
          xhr = $.getJSON('https://suggestqueries.google.com/complete/search?client=firefox&ds=yt', {
            q: term,
            dataType: "jsonp"
          }, function(data) {
            console.log(data)
            response(data);
          });
      }
  });

gives me the response:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://suggestqueries.google.com/complete/search?client=firefox&ds=yt&q=test&dataType=jsonp. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

Am I just not allowed to do this?

Community
  • 1
  • 1
stackers
  • 2,701
  • 4
  • 34
  • 66

4 Answers4

0

install a chrome plugin "Allow-Control-Allow-Origin"

Rex
  • 137
  • 2
  • 12
0

To prevent Cross Site Scripting (XSS) attacks, XHR's are limited in the domains that they can contact. But there are ways around this that also keep users secure.

  1. Route the request through your own domain, but this depends on your server side architecture

  2. If developing a browser extension, like in chrome it is possible to configure the manifest to allow the communication

  3. Or, enable cross-origin requests by modifying headers sent by the server, like in

    1. PHP
    2. Node.js

I would not suggest asking users to install extensions that usurp this very important policy

Community
  • 1
  • 1
Jason
  • 779
  • 1
  • 9
  • 30
0

Was able to figure it out thanks to this answer: https://stackoverflow.com/a/6120260/929321

Changed from .getJSON to .ajax and added dataType: 'jsonp'.

$.ajax({

    url: 'https://www.googleapis.com/moderator/v1/series?key='+key,
    data: myData,
    type: 'GET',
    crossDomain: true,
    dataType: 'jsonp',
    success: function() { alert("Success"); },
    error: function() { alert('Failed!'); },
    beforeSend: setHeader
});
Community
  • 1
  • 1
stackers
  • 2,701
  • 4
  • 34
  • 66
0

Try the provided youtube search API by getting the credentials from the API console. Here is the link and you can try it out there as well : https://developers.google.com/youtube/v3/docs/search/list

$.get(
   "https://www.googleapis.com/youtube/v3/search",{
   part : 'snippet',
   q : 'batman',
   key: 'YOUR_API_KEY'},
   function(data) {
      console.log(data);
     //do the manipulation here
  }
);
acesmndr
  • 8,137
  • 3
  • 23
  • 28