找不到Microsoft QnA Maker API错误资源

时间:2017-06-09 19:08:10

标签: javascript json api azure microsoft-cognitive

我正在使用JS为Micosoft QnA Maker API发送POST请求。但是它返回了一个没有找到错误资源的JSON文件

{ "error": { "code": "ResourceNotFound", "message": "The requested resource was not found." } }

虽然我根据API做的一切都是正确的,但仍然会出现同样的错误。

我正在使用JS中的代码,如下所示:

<script type="text/javascript">
$(function() {
    var params = {

"question": "is qna maker free?",
"top": 3
    };

    $.ajax({
        url: "https://westus.api.cognitive.microsoft.com/qnamaker/v2.0/knowledgebases/XXXX/generateAnswer?" + $.param(params),
        beforeSend: function(xhrObj){
            // Request headers
            xhrObj.setRequestHeader("Content-Type","application/json");
            xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","XXXX");
        },
        type: "POST",
        // Request body
        data: "{body}",
    })
    .done(function(data) {
        alert("success");
    })
    .fail(function() {
        alert("error");
    });
});

此代码返回警告(&#34;错误&#34;),并在控制台中显示以下消息:

westus.api.cognitive.microsoft.com/qnamaker/v2.0/knowledgebases/XXXX/generateAnswer?question=is+qna+maker+free%3F&top=3:1 POST https://westus.api.cognitive.microsoft.com/qnamaker/v2.0/knowledgebases/XXXX/generateAnswer?question=is+qna+maker+free%3F&top=3 400 (Bad Request)

我指的是link

我已经看到,响应400说&#34;没有指定参数问题。&#34;但我已经指出了这个问题。 我的代码出了什么问题?我很确定在我的代码中一定有问题。

"question": "is qna maker free?",
"top": 3

当我访问此link

它还显示带有错误的相同JSON文件。

1 个答案:

答案 0 :(得分:1)

你的代码中有:

$.ajax({
    url: "https://westus.api.cognitive.microsoft.com/qnamaker/v2.0/knowledgebases/XXXX/generateAnswer?" + $.param(params),
    beforeSend: function(xhrObj){
        // Request headers
        xhrObj.setRequestHeader("Content-Type","application/json");
        xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","XXXX");
    },
    type: "POST",
    // Request body
    data: params, //replace {body} with the params variable
    dataType: "json" //add this line
})

您可能还希望使用成功/错误函数处理响应,因此它将如下所示:

$.ajax({
    url: "https://westus.api.cognitive.microsoft.com/qnamaker/v2.0/knowledgebases/XXXX/generateAnswer?" + $.param(params),
    beforeSend: function(xhrObj){
        // Request headers
        xhrObj.setRequestHeader("Content-Type","application/json");
        xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","XXXX");
    },
    type: "POST",
    data: params,
    dataType: "json",
    success: function(data){
    alert("success");
    },
    error: function(e){
    console.log(e);
    }
})