hooray4horus
hooray4horus

Reputation: 99

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. Can anyone tell me what I am doing wrong?

Upvotes: 1

Views: 1069

Answers (1)

Wainage
Wainage

Reputation: 5412

A quick check with Postman shows you're getting a bad parameter (not 404).

Couple of things:

  1. returnFaceAttributes should be "age" (not "{age}")
  2. data needs a param name

Try this (check the data update):

<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).

One last important note. Change your Ocp-Apim-Subscription-Key key immediately!

Upvotes: 5

Related Questions