0

这个通过谷歌 API 进行的简单网络搜索是不稳定的。有时它会返回 4 个第一个发现(应该如此),有时 JSON 认为它是“成功的”,但 responseData 为空。为什么我会遇到这些不一致?这是一个异步问题吗?如何让它更稳定?(当我在谷歌上搜索图像时,它很稳定)

var baseUrl = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&start=0&q="; 
var searchTerm = "obama"; //Lots of hits

$(document).ready(function() // don't do anything until the document is loaded.
{  
$.getJSON(baseUrl + searchTerm +  "&callback=?", function(json) // call getJSON providing the complete url with search term and a JSONP callback
    {
    $.each(json.responseData.results, function(i, gResults){  
        console.log("title: " + gResults.titleNoFormatting);
    });
  });
});

当它失败时,我在 json 数据结构中找到了这个:

json.responseDetails: "Suspected Terms of Service Abuse. Please see
  http://code.google.com/apis/errors"

所以谷歌认为我用太多的请求来攻击它。我必须设置 API 密钥吗?现在我只包括

<meta name="google-site-verification" content="myAPIkey-Herevbng66r" />

但是我在本地计算机上运行,​​所以可能没有帮助……</p>

4

1 回答 1

0

尝试这个:

function(json) // call getJSON providing the complete url with search term and a JSONP callback
    {
       if (json.responseData === null)
          console.log("json returned nothing");
       else
         $.each(json.responseData.results, function(i, gResults){  
             console.log("title: " + gResults.titleNoFormatting);
         });
  });
});
于 2011-09-16T01:43:05.703 回答