我有这个代码:
<html>
<head>
<title></title>
<script src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('search', '1');
google.setOnLoadCallback(OnLoad);
var search;
//i suggest instead of this to make keywords list so first to pick random keyword than to do search and pick random image
var keyword = 'cute kittens';
function OnLoad()
{
search = new google.search.ImageSearch();
search.setSearchCompleteCallback(this, searchComplete, null);
search.execute(keyword);
}
function searchComplete()
{
if (search.results && search.results.length > 0)
{
var rnd = Math.floor(Math.random() * search.results.length);
//you will probably use jQuery and something like: $('body').css('background-image', "url('" + search.results[rnd]['url'] + "')");
document.body.style.backgroundImage = "url('" + search.results[rnd]['url'] + "')";
}
}
</script>
</head>
<body>
它应该给我一张小猫的照片,然后把它作为背景,但它没有给我任何背景,是否有人知道如何解决这个问题?如果你这样做,请帮忙。这可能看起来像一个重复,但我得到了以前的帖子中的代码,它不起作用,所以请不要标记为重复,我已尝试以多种方式修改代码,没有工作,我丢失了不同的编辑时,我设备关闭,所以我没有那些。 (我是一个糟糕的开发者,并没有保存)
自2011年以来,该API似乎已被弃用.Google可能会完全关闭该服务,因为他们的弃用政策允许他们根据以下某个条款执行此操作
如果符合以下条件,Google保留在不通知的情况下立即停止提供全部或部分不赞成版本服务的权利:
https://developers.google.com/terms/deprecation
我把你的代码放在JSfiddle中:https://jsfiddle.net/zcthpd4L/6/并遇到了错误:
Uncaught TypeError: Cannot read property 'ImageSearch' of undefined
at OnLoad ((index):52)
at (index):70
这似乎不是由于配置问题,并且可能是由于服务不再可用。您最好的选择是使用自定义搜索API:https://developers.google.com/custom-search/