简体   繁体   English

如何向 microsoft vision api 发送 ajax 请求?

[英]How do I send an ajax request to microsoft vision api?

I am following the documentation here and the sample code at the bottom looks like this我正在关注此处的文档,底部的示例代码如下所示

<!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
        "returnFaceId": "true",
        "returnFaceLandmarks": "false",
        "returnFaceAttributes": "{age}",
    };

    $.ajax({
        url: "https://api.projectoxford.ai/face/v1.0/detect?" + $.param(params),
        beforeSend: function(xhrObj){
            // Request headers
            xhrObj.setRequestHeader("Content-Type","application/json");
            xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","REDACTED");
        },
        type: "POST",
        // Request body
        data: "http://newsrescue.com/wp-content/uploads/2015/04/happy-person.jpg",
    })
    .done(function(data) {
        alert("success");
    })
    .fail(function() {
        alert("error");
    });
});
</script>
</body>
</html>

But i keep getting error code 404 resource not found.但我不断收到错误代码 404 资源未找到。 Can anyone tell me what I am doing wrong?谁能告诉我我做错了什么?

A quick check with Postman shows you're getting a bad parameter (not 404). Postman的快速检查表明您得到了一个错误的参数(不是 404)。

Couple of things:几件事:

  1. returnFaceAttributes should be "age" (not "{age}" ) returnFaceAttributes应该是"age" (不是"{age}"
  2. data needs a param name data需要一个参数名称

Try this (check the data update):试试这个(检查data更新):

<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
    <title>JSSample</title>

</head>
<body>

<script type="text/javascript">
    $(function() {
    var params = {
        // Request parameters
        "returnFaceId": "true",
        "returnFaceLandmarks": "false",
        "returnFaceAttributes": "age",
    };

    $.ajax({
        url: "https://api.projectoxford.ai/face/v1.0/detect?" + $.param(params),
        beforeSend: function(xhrObj){
            // Request headers
            xhrObj.setRequestHeader("Content-Type","application/json");
            xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","e2c75ad5d44846d590ac7c2dcc2f210e");
        },
        type: "POST",
        // Request body
        data: '{ "url": "http://newsrescue.com/wp-content/uploads/2015/04/happy-person.jpg"}'
    })
    .done(function(data) {
        console.log(data);
        alert("success");
    })
    .fail(function() {
        alert("error");
    });
});
</script>
</body>
</html>

I created a jsbin to make sure it works (she's 19.3 years old according to Microsoft).我创建了一个jsbin以确保它正常工作(根据微软的说法,她 19.3 岁)。

One last important note.最后一个重要说明。 Change your Ocp-Apim-Subscription-Key key immediately!立即更改您的Ocp-Apim-Subscription-Key密钥!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM