0

I am trying below snippet of code for face identify of azure samples with proper Subscription-Key. I get bad request 400 - can any one please help me how to send request body to work for this ajax call.

 <!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
        };

        $.ajax({
            url: "https://api.cognitive.azure.cn/face/v1.0/identify?" + $.param(params),
            beforeSend: function(xhrObj){
                // Request headers
                xhrObj.setRequestHeader("Content-Type","application/json");
                xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","MY_ACCESS_KEY");
            },
            type: "POST",
            // Request body
            data: "{body}",
        })
        .done(function(data) {
            alert("success");
        })
        .fail(function() {
            alert("error");
        });
    });
</script>
</body>
</html>
SaKol
  • 99
  • 10
  • You shouldn't provide your private Ocp-Apim-Subscription-Key in public :). You may want to remove that with some string such as MY_ACCESS_KEY. – Ronak Nov 18 '17 at 20:16

2 Answers2

0

The url seems to be bad here. Two things: 1) You need to append location before api.cognitive.microsoft.com and 2). .cn should be .com. Your url may look like this, depending on your location:

 url: "https://westus.api.cognitive.azure.com/face/v1.0/identify?"

More locations and details are here: https://eastasia.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395239

Ronak
  • 751
  • 5
  • 10
  • I need to know what this mean in request. data: "{body}", – SaKol Nov 19 '17 at 20:44
  • It just takes the "body" part of the JSON response and removes header info that may have sensitive info (such as access key/optional Client ID, etc.). You can use this info for console/other output w/o worrying about any header info. – Ronak Nov 20 '17 at 19:20
0

Try

data: JSON.stringify({name: "Test"})
rysahara
  • 346
  • 1
  • 6
  • 23
  • 3
    Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others. – lucascaro Oct 30 '18 at 03:21