认知服务响应不良请求

时间:2017-11-22 01:37:11

标签: javascript jquery html ajax microsoft-cognitive

我不明白以下代码出了什么问题,我正在使用面向API的MS-Cognitive Services以及将图像转换为blob(代码如下)

var ab = [];
var img = new Image();
img.src = imageUri; // imageUri for FilePath
img.setAttribute('crossOrigin', 'anonymous');
var canvas = document.createElement("canvas");
canvas.width = $(window).width();
canvas.height = $(window).height();
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
var dataURL = canvas.toDataURL("image/jpeg");
alert(dataURL.replace(/^data:image\/(png|jpg);base64,/, ""));

var BASE64_MARKER = ';base64,';
var parts = dataURL.split(BASE64_MARKER);
var contentType = parts[0].split(':')[1];
var raw = window.atob(parts[1]);
var rawLength = raw.length;
var uInt8Array = new Uint8Array(rawLength);
    for (var i = 0; i < rawLength; ++i) {
        uInt8Array[i] = raw.charCodeAt(i);
    }   var bb = new Blob([new Uint8Array(ab)]); 

遵循Code进行面部检测的Ajax调用(MS-Cognitive Services)

var params = {
    "returnFaceId": "true",
    "returnFaceLandmarks": "false",
    "returnFaceAttributes": "{string}",
};

$.ajax({
    url: "https://australiaeast.api.cognitive.microsoft.com/face/v1.0/detect?" + $.param(params),
    beforeSend: function (xhrObj) {
        xhrObj.setRequestHeader("Content-Type", "application/octet-stream");
        xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key", "KEY_GOES_HERE");
    },
    type: "POST",
    data: bb,
    processData: false,
})
    .done(function (data) {
        alert("Response Done::" +data);
    })
    .fail(function (jqXHR, textStatus, errorThrown) {
        alert(" Status:: " + jqXHR.status + "  ResponseText is:: " + jqXHR.statusText + "");
    });

我收到响应为400-Bad Request。我不明白我在哪里弄错了。?顺便说一句,整个事情是相同的功能。 有人可以帮我吗?谢谢。

1 个答案:

答案 0 :(得分:0)

解决了这个问题

var params = {
    // Request parameters
    "returnFaceId": "true",
    "returnFaceLandmarks": "false",
    "returnFaceAttributes": "age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise",
};