javascript - SyntaxError: JSON.parse: unexpected character with youtube api search -


i'm using google api search javascript code searching videos in webpage, bit modified. leave codes use searching:

search.js

/*global gapi*/  // after api loads, call function enable search box. function handleapiloaded() {     $('#search-button').attr('disabled', false); }  // search specified string. function search() {     var q = $('#query').val();     var request = gapi.client.youtube.search.list({         q: q,         part: 'snippet'     });      request.execute(function(response) {         var str = json.stringify(response.result);         $('#search-container').html('<pre>' + str + '</pre>');     }); } // search specified string. function searchget() {     var q = $('#query').val();      var url = "https://www.googleapis.com/youtube/v3/search?part=snippet&q="+q+"here_my_api_key";      $.get(url, function(response) {         var str = json.stringify(response.result);         $('#search-container').html('<pre>' + str + '</pre>');     }); } 

where have put here_my_api_key api key, ihave removed it. have other file search.html containing this:

search.html

<!doctype html> <html> <head>     <title>search</title> </head> <body> <div id="buttons">     <label>         <input id="query" value='cats' type="text"/>         <button id="search-button" disabled onclick="searchget()">search</button>     </label> </div> <div id="search-container"> </div> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> <script src="search.js"></script> <script src="//apis.google.com/js/client.js?onload=handleapiloaded"></script> </body> </html> 

this gives me undefined error, , this, ihave supposed have change json.stringify json.parse, so, when changing it, gives me error:

syntaxerror: json.parse: unexpected character  var str = json.parse(response.result); 

but in console, can see search query --> http://screencloud.net/v/ti3m

then, what's wrong it? can if want show videos of search query? thanks!

well, use next code ( check key , permissions )

<!doctype html> <html> <head>     <title>search</title> </head> <body> <div id="buttons">     <label>         <input id="query" value='cats' type="text"/>         <button id="search-button" disabled onclick="searchget()">search</button>     </label> </div> <div id="search-container"> </div> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> <script>   /*global gapi*/  // after api loads, call function enable search box. function handleapiloaded() {     $('#search-button').attr('disabled', false); }  // search specified string. function search() {     var q = $('#query').val();     var request = gapi.client.youtube.search.list({         q: q,         part: 'snippet'     });      request.execute(function(response) {         var str = json.stringify(response.result);         $('#search-container').html('<pre>' + str + '</pre>');     }); } // search specified string. function searchget() {     var q = $('#query').val();      var url = "https://www.googleapis.com/youtube/v3/search?part=snippet&q="+q+"&key=aizasyb1umympwfobrabjkgcf7yrf6rmug6suey";      $.get(url, function(response) {         var str = json.stringify(response);         $('#search-container').html('<pre>' + str + '</pre>');     }); } </script> <script src="//apis.google.com/js/client.js?onload=handleapiloaded"></script> </body> </html> 

try here

and result

{"kind":"youtube#searchlistresponse","etag":"\"bdc7vthym9nfosqm1_koyhtjtew/cwfaj1oekuon11qi2acxuhjijwm\"","nextpagetoken":"cauqaa","pageinfo":{"totalresults":1000000,"resultsperpage":5},"items":[{"kind":"youtube#searchresult","etag":"\"bdc7vthym9nfosqm1_koyhtjtew/m584anoryyzmpzvsmjyqub5jenu\"","id":{"kind":"youtube#channel","channelid":"uc9egiwujsqz0cy2to5fvsiq"},"snippet":{"publishedat":"2013-11-29t09:26:45.000z","channelid":"uc9egiwujsqz0cy2to5fvsiq","title":"cat - topic","description":"the domestic cat small, furry, domesticated, , carnivorous mammal. called housecat when kept indoor pet, or c...","thumbnails":{"default":{"url":"https://i.ytimg.com/i/9egiwujsqz0cy2to5fvsiq/1.jpg"},"medium":{"url":"https://i.ytimg.com/i/9egiwujsqz0cy2to5fvsiq/mq1.jpg"},"high":{"url":"https://i.ytimg.com/i/9egiwujsqz0cy2to5fvsiq/hq1.jpg"}},"channeltitle":"","livebroadcastcontent":"none"}},{"kind":"youtube#searchresult","etag":"\"bdc7vthym9nfosqm1_koyhtjtew/w9fyp9wl4edzxhvmdz7_nqtad6i\"","id":{"kind":"youtube#video","videoid":"7w6riv_kaqm"},"snippet":{"publishedat":"2014-03-09t12:57:29.000z","channelid":"ucky3mg7_if9klvuvw3rpmfw","title":"funny cats massaging , petting dogs - cute animal compilation","description":"some cats love massage , pet dogs, wonder why :p , looks dogs don't mind @ all, love it! there of cours...","thumbnails":{"default":{"url":"https://i.ytimg.com/vi/7w6riv_kaqm/default.jpg"},"medium":{"url":"https://i.ytimg.com/vi/7w6riv_kaqm/mqdefault.jpg"},"high":{"url":"https://i.ytimg.com/vi/7w6riv_kaqm/hqdefault.jpg"}},"channeltitle":"wloltigerlolw2","livebroadcastcontent":"none"}},{"kind":"youtube#searchresult","etag":"\"bdc7vthym9nfosqm1_koyhtjtew/tiayne-qv58tkdffi2khu_jnk_o\"","id":{"kind":"youtube#video","videoid":"fkkejo2dmv4"},"snippet":{"publishedat":"2013-12-31t08:37:23.000z","channelid":"ucfcxwyxwh3nivdjavurclwq","title":"funny cats compilation 20 min - happy new year 2014","description":"happy new year - cats compilation.","thumbnails":{"default":{"url":"https://i.ytimg.com/vi/fkkejo2dmv4/default.jpg"},"medium":{"url":"https://i.ytimg.com/vi/fkkejo2dmv4/mqdefault.jpg"},"high":{"url":"https://i.ytimg.com/vi/fkkejo2dmv4/hqdefault.jpg"}},"channeltitle":"anacalderon1","livebroadcastcontent":"none"}},{"kind":"youtube#searchresult","etag":"\"bdc7vthym9nfosqm1_koyhtjtew/jdimyoezvltntqlwapsgyz3dhxm\"","id":{"kind":"youtube#video","videoid":"qdiz_vzvj0w"},"snippet":{"publishedat":"2014-03-09t16:58:58.000z","channelid":"uc7hs0659yc82h7snhjnfd4g","title":"black cats - cheghad asheghetam official video hd","description":"join black cats & favorite artists: http://fb.com/avangmusic artist: black cats song: cheghad asheghetam label: avang music director: alex ...","thumbnails":{"default":{"url":"https://i.ytimg.com/vi/qdiz_vzvj0w/default.jpg"},"medium":{"url":"https://i.ytimg.com/vi/qdiz_vzvj0w/mqdefault.jpg"},"high":{"url":"https://i.ytimg.com/vi/qdiz_vzvj0w/hqdefault.jpg"}},"channeltitle":"avangmusic","livebroadcastcontent":"none"}},{"kind":"youtube#searchresult","etag":"\"bdc7vthym9nfosqm1_koyhtjtew/1q6jkbgdyllplxc6kxyyq9-bdjm\"","id":{"kind":"youtube#video","videoid":"kdgt1zhkvnm"},"snippet":{"publishedat":"2013-02-06t13:37:46.000z","channelid":"ucb91zl2mxlmuwrinockeynq","title":"epic funny cats 20 minutes","description":"more funny cats -- http://www.catsbook.co epic funny cats 20 minutes http://www.catsbook.co http://www.facebook.com/catsbook.co ...","thumbnails":{"default":{"url":"https://i.ytimg.com/vi/kdgt1zhkvnm/default.jpg"},"medium":{"url":"https://i.ytimg.com/vi/kdgt1zhkvnm/mqdefault.jpg"},"high":{"url":"https://i.ytimg.com/vi/kdgt1zhkvnm/hqdefault.jpg"}},"channeltitle":"crazycats2020","livebroadcastcontent":"none"}}]} 

at page


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -