【问题标题】:How to use image search api [BING]如何使用图片搜索 api [BING]
【发布时间】:2016-07-23 02:21:41
【问题描述】:

我正在尝试使用图像搜索 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}" 看起来不正确 - 您是否有指向您获取此代码的页面的链接?
  • url 是 [link]dev.cognitive.microsoft.com/docs/services/… [/link] 我尝试使用ˋconsole.log(data)ˋ 没有结果
  • 奇怪你在除 C# 之外的所有语言中都需要这个"{body}" ...控制台上的任何错误或消息

标签: javascript ajax bing


【解决方案1】:

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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-30
    • 2014-11-04
    • 1970-01-01
    • 1970-01-01
    • 2019-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多