JSON在Javascript中失败但POSTMAN有效

时间:2017-06-05 16:09:13

标签: jquery json botframework

这适用于邮递员,但Javascript每次都失败! 谁知道我做错了什么?

代码示例来自此处https://westus.dev.cognitive.microsoft.com/docs/services/58994a073d9e04097c7ba6fe/operations/58994a073d9e041ad42d9ba9

    $(function() {
        var params = {     
        }; 
        $.ajax({
            url: "https://westus.api.cognitive.microsoft.com/qnamaker/v2.0/knowledgebases/dfb79d2e-4f4d-4201-8511-b9d7ab08bcb1/generateAnswer?" + $.param(params),
            beforeSend: function(xhrObj){
                // Request headers
                xhrObj.setRequestHeader("Content-Type","application/json");
                xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key", key);
            },
            type : "POST",
            data : {"question" : "Hi"}
        })
        .done(function(data) {
            alert("success");
        })
        .fail(function (jqXHR, textStatus, error) {
        alert("Post error: " + error);
    });
});

- 控制台日志---

  

POST XHR   https://westus.api.cognitive.microsoft.com/qnamaker/v2.0/knowledgebases/dfb79d2e-4f4d-4201-8511-b9d7ab08bcb1/generateAnswer [HTTP / 1.1 400 Bad Request 377ms] Headers Params POST Response Call   堆   ErrorObjectCode" BadArgument"消息" [{"键":"查询""值":["" ]}]"   {"错误" {"代码":" BadArgument""消息":" [{\"键\":\"查询\" \"值\":[\" \"]}]"}}

1 个答案:

答案 0 :(得分:0)

  

经过大量的反复试验,我才能让它发挥作用;只是为了记录   有关此主题的Microsoft文档无效。

    $.ajax({
        url: "https://westus.api.cognitive.microsoft.com/qnamaker/v2.0/knowledgebases/dfb79d2e-4f4d-4201-8511-b9d7ab08bcb1/generateAnswer",
        beforeSend: function(xhrObj){
            // Request headers
            xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","theKey");
        },
        type : "POST",
        data : {"question": "Hi"},
        dataType: "json",
        crossDomain: true
    })
    .done(function(data) {
        alert("success");
    })
    .fail(function (jqXHR, textStatus, error) {
        console.log(jqXHR);
    });

JSFiddle demo