2017-06-05 58 views
0

这从邮递员的作品,但Javascript每次都失败! 任何知道我做错了什么?JSON在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); 
    }); 
}); 

--console日志---

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 Stack ErrorObjectCode"BadArgument"Message"[{"Key":"query","Value":[""]}]" {"Error":{"Code":"BadArgument","Message":"[{\"Key\":\"query\",\"Value\":[\"\"]}]"}}

+0

“发ils“有点模糊 - 你是否在控制台中收到错误信息?你得到失败()回调?失败中的'textStatus'值是什么(你没有在代码片段中)? https://stackoverflow.com/a/9847328/2181514 –

+0

失败是“发布错误:错误的请求” –

+1

你确定你的'Ocp-Apim-Subscription-Key'应该是公共知识吗? –

回答

0

After a lot of trial and error, I was able to make it work; just for the record the Microsoft documentation on the subject, is not working.

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

相关问题