2017-01-18 11 views
0

Javascriptを使用しているText Analytics 400のリクエストエラーの問題で、同様の質問が見つかりません。 Javascriptの下の簡単なコードがエラーを返しました。送信されたPOSTが有効であることを確認しました。私はあなたの親切な助けが必要です。Javascriptでテキストアナリティクスを呼び出すとHTTP 400が正しくない要求エラーが返される

var params = { 
    "documents": [ 
     { 
      "language": "en", 
      "id": "1", 
      "text": "This is my first test. All is good" 
     }, 
     { 
      "language": "en", 
      "id": "2", 
      "text": "This is my first test. All is not good" 
     }, 
     { 
      "language": "en", 
      "id": "3", 
      "text": "Why is this not working as expected?" 
     }, 
     { 
      "language": "en", 
      "id": "4", 
      "text": "You got to be kidding me like this" 
     }, 
     { 
      "language": "en", 
      "id": "5", 
      "text": "I hope this will finally work. I hope it will" 
     } 
    ] 
} 

$.ajax({ 
    url: "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment?" + $.param(params), 
    beforeSend: function(xhrObj){ 
     // Request headers 
     xhrObj.setRequestHeader("Content-Type","application/json"); 
     xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","MY API KEY"); 
    }, 
    type: "POST", 
    // Request body 
    data: "{body}", 
}) 
.done(function(data) { 
    console.log(data); 
}) 
.fail(function() { 
    alert("error"); 
}); 
+0

このapiは常に404を返します。https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment? – Vincent

答えて

0

@Redz、 実装については、このフィドルを参照してください。それは「YOUR_KEY_HERE」と言うところ単にあなたの鍵を交換うまく機能:

var params = { 
       "documents": [ 
        { 
         "language": "en", 
         "id": "1", 
         "text": "This is my first test. All is good" 
        }, 
        { 
         "language": "en", 
         "id": "2", 
         "text": "This is my first test. All is not good" 
        }, 
        { 
         "language": "en", 
         "id": "3", 
         "text": "Why is this not working as expected?" 
        }, 
        { 
         "language": "en", 
         "id": "4", 
         "text": "You got to be kidding me like this" 
        }, 
        { 
         "language": "en", 
         "id": "5", 
         "text": "I hope this will finally work. I hope it will" 
        } 
       ] 
      }; 

      $.ajax({ 
       method: 'POST', 
       url: "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment", 
       headers:{ 
        "Content-Type":"application/json", 
        "Ocp-Apim-Subscription-Key":"YOUR_KEY_HERE", 
        "Accept":"application/json" 
       }, 
       data: JSON.stringify(params), 
       dataType: 'text', 
      }) 
      .done(function(data) { 
       console.log('Here: ' + data); 
       $('#responseData').html(data); 
      }) 
      .fail(function(data) { 
       alert("error" + JSON.stringify(data)); 
      }); 

グッドラック:後https://jsfiddle.net/expcc0f5/1/

が正常に動作するコードです。それがあなたのためにどのように行ったか聞いて大好きです。

関連する問題