如何使用图像搜索API [BING]

3

我正在尝试使用图像搜索API,但是没有获得搜索结果。 这是我在文档同一页找到的代码。

    <!DOCTYPE html>
<html>
<head>
    <title>JSSample</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<body>

<script type="text/javascript">
    $(function() {
        var params = {
            // Request parameters
            "q": "cats",
            "count": "10",
            "offset": "0",
            "mkt": "en-us",
            "safeSearch": "Moderate",
        };

        $.ajax({
            url: "https://api.cognitive.microsoft.com/bing/v5.0/images/search?" + $.param(params),
            beforeSend: function(xhrObj){
                // Request headers
                xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","MYKEY");
            },
            type: "GET",
            // Request body
            data: "{body}",
        })
        .done(function(data) {
            alert("success");
        })
        .fail(function() {
            alert("error");
        });
    });
</script>
</body>
</html>

我非常希望能够进行图片搜索,而这个API可以为我提供服务。


"data: "{body}"这看起来不对劲 - 你有这段代码来源页面的链接吗?" - Jaromanda X
链接为[link]https://dev.cognitive.microsoft.com/docs/services/56b43eeccf5ff8098cef3807/operations/56b4447dcf5ff8098cef380d[/link]。我尝试使用“console.log(data)”但没有结果。 - Luis Ramos
很奇怪,在除了C#之外的所有语言中,你都需要这个"{body}"...在控制台上会出现任何错误或消息。 - Jaromanda X
1个回答

0
Jaromanda X 是对的,你可以删除 {body},它应该可以工作。我正在使用相同的代码,它可以正常工作。
<!DOCTYPE html>
<html>
<head>
    <title>JSSample</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<body>

<script type="text/javascript">
    $(function() {
        var params = {
            // Request parameters
            "q": "cats"
        };

        $.ajax({
            url: "https://api.cognitive.microsoft.com/bing/v5.0/images/search?" + $.param(params),
            beforeSend: function(xhrObj){
                // Request headers
                xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","MYKEY");
            },
            type: "GET",
            // Request body
            data: "",
        })
        .done(function(data) {
            alert("success");
        })
        .fail(function() {
            alert("error");
        });
    });
</script>
</body>
</html>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接