【问题标题】:SyntaxError: JSON.parse: unexpected character with youtube api searchSyntaxError:JSON.parse:youtube api 搜索出现意外字符
【发布时间】:2014-04-13 11:52:46
【问题描述】:

我使用google api search javascript code 在我的网页中搜索视频,但做了一些修改。我留下用于搜索的代码:

search.js

/*global gapi*/

// After the API loads, call a function to enable the search box.
function handleAPILoaded() {
    $('#search-button').attr('disabled', false);
}

// Search for a 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 for a 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>');
    });
}

我把 HERE_MY_API_KEY 放在哪里是我的 API 密钥,但我已经删除了它。然后我有另一个文件search.html 包含这个:

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>

这给了我一个undefined 错误,因此,我想我必须用JSON.parse 更改JSON.stringify,所以,在更改它时,它给了我另一个错误:

SyntaxError: JSON.parse: unexpected character

var str = JSON.parse(response.result);

但在控制台中,我可以看到搜索查询 --> http://screencloud.net/v/tI3m

那么,它有什么问题呢?如果我想让它显示我搜索查询的所有视频,我该怎么办? 谢谢!

【问题讨论】:

    标签: javascript json youtube


    【解决方案1】:

    好吧,我使用下一个代码(检查您的密钥和权限)

    <!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 the API loads, call a function to enable the search box.
    function handleAPILoaded() {
        $('#search-button').attr('disabled', false);
    }
    
    // Search for a 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 for a 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>
    

    试试here

    得到结果

    {"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 is a small, usually furry, domesticated, and carnivorous mammal. It is often called the housecat when kept as an indoor pet, or simply the 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 and petting dogs - Cute animal compilation","description":"Some cats really love to massage and pet dogs, I wonder why they to that :P And looks like dogs don't mind that at all, they love it! Well there are 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 & all your 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"}}]}
    

    在页面

    【讨论】:

    • 谢谢!有效!它输出 youtube 视频对象,就像我想要的一样!
    【解决方案2】:

    我检查了你的代码 - 使用

    $.get(url, function(response) {
        var str = JSON.stringify(response);
        $('#search-container').html('<pre>' + str + '</pre>');
    });
    

    代替你的代码

    $.get(url, function(response) {
        var str = JSON.stringify(response.result);
        $('#search-container').html('<pre>' + str + '</pre>');
    });
    

    导致jquery将解析后的数据放到成功回调函数中

    【讨论】:

    • 感谢您的回复!我试过了,但它不起作用......我还能做什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2015-01-17
    • 1970-01-01
    • 2020-06-04
    • 2019-06-11
    • 1970-01-01
    • 2021-08-19
    相关资源
    最近更新 更多