2015-10-30 86 views
2

好了,我想使用谷歌API的圖像搜索,我知道已經過時,但我不希望使用自定義搜索在我的谷歌圖片搜索無圖像

這是我的代碼」一直在使用

<script type="text/javascript"> 

    google.load('search', '1'); 

    var imageSearch; 

    function addPaginationLinks() { 

    // To paginate search results, use the cursor function. 
    var cursor = imageSearch.cursor; 
    var curPage = cursor.currentPageIndex; // check what page the app is on 
    var pagesDiv = document.createElement('div'); 
    for (var i = 0; i < cursor.pages.length; i++) { 
     var page = cursor.pages[i]; 
     if (curPage == i) { 

     // If we are on the current page, then don't make a link. 
     var label = document.createTextNode(' ' + page.label + ' '); 
     pagesDiv.appendChild(label); 
     } else { 

     // Create links to other pages using gotoPage() on the searcher. 
     var link = document.createElement('a'); 
     link.href="/image-search/v1/javascript:imageSearch.gotoPage("+i+');'; 
     link.innerHTML = page.label; 
     link.style.marginRight = '2px'; 
     pagesDiv.appendChild(link); 
     } 
    } 

    var contentDiv = document.getElementById('content'); 
    contentDiv.appendChild(pagesDiv); 
    } 

    function searchComplete() { 

    // Check that we got results 
    if (imageSearch.results && imageSearch.results.length > 0) { 

     // Grab our content div, clear it. 
     var contentDiv = document.getElementById('content'); 
     contentDiv.innerHTML = ''; 

     // Loop through our results, printing them to the page. 
     var results = imageSearch.results; 
     for (var i = 0; i < results.length; i++) { 
     // For each result write it's title and image to the screen 
     var result = results[i]; 
     var imgContainer = document.createElement('div'); 
     var title = document.createElement('div'); 

     // We use titleNoFormatting so that no HTML tags are left in the 
     // title 
     title.innerHTML = result.titleNoFormatting; 
     var newImg = document.createElement('img'); 

     // There is also a result.url property which has the escaped version 
     newImg.src="/image-search/v1/result.tbUrl;" 
     imgContainer.appendChild(title); 
     imgContainer.appendChild(newImg); 

     // Put our title + image in the content 
     contentDiv.appendChild(imgContainer); 
     } 

     // Now add links to additional pages of search results. 
     addPaginationLinks(imageSearch); 
    } 
    } 

    function OnLoad() { 

    // Create an Image Search instance. 
    imageSearch = new google.search.ImageSearch(); 

    // Set searchComplete as the callback function when a search is 
    // complete. The imageSearch object will have results in it. 
    imageSearch.setSearchCompleteCallback(this, searchComplete, null); 

    // Find me a beautiful car. 
    imageSearch.execute("Ferrari"); 

    // Include the required Google branding 
    google.search.Search.getBranding('branding'); 
    } 
    google.setOnLoadCallback(OnLoad); 
</script> 

的問題是,當我運行它時,圖像不會出現

我知道,問題是,它使用的URL不起作用

問題是這行代碼

newImg.src="/image-search/v1/result.tbUrl;" 

但我不知道是否有其他網址我可以使用或我能做些什麼

我不能使用JQuery,這對於一個College Proyect,我們不能使用JQuery

感謝您的關注!

+0

是否使用他們給你的ID?我們複製和粘貼的一些代碼有一個需要替換的通用標識。 – Rodrigo

+0

我有同樣的問題。沒有顯示結果。當我顯示日誌時,我沒有任何結果'imageSearch.results' – Intuitisoft

+0

GimageSearch上的XHR調用返回'callback google.search.ImageSearch.RawCompletion('1',null,403,'此API不再可用。 ',200)'我認爲我們不能使用這個API – Intuitisoft

回答

-1

變化

link.href="/image-search/v1/javascript:imageSearch.gotoPage("+i+');'; 

link.href="javascript:imageSearch.gotoPage("+i+');'; 

,並更改

newImg.src="/image-search/v1/result.tbUrl;" 

newImg.src=result.tbUrl; 
+0

我嘗試了更新後的(?)「V1」,沒有任何效果。我也沒有得到任何形象,他們在https://developers.google.com/image-search/v1/devguide – Rodrigo

+0

上提供的「hello world」示例爲什麼downvote?在應用上述更改後,代碼將起作用。如果更改對您沒有任何影響,請提供您的代碼。 – user1438038

0

的谷歌截至2011年5月26日,圖像搜索API已被正式棄用,顯然完全停止。

上GimageSearch的XHR調用返回callback google.search.ImageSearch.RawCompletion('1', null, 403, 'This API is no longer available.', 200)

必須使用新的自定義搜索API:https://developers.google.com/custom-search/