2

Ich versuche, Watson-Text zu Sprache-Dienst in einem JavaScript-Code zu verwenden. Allerdings stecke ich fest, um es zum Laufen zu bringen.Berechtigungsproblem für Watson Text to Speech von Java-Skript

Wenn ich den folgenden Code verwenden:

$.ajax({ 
       url: 'https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize', 
       type: 'POST', 
       headers: {"Content-Type": "application/json", "Accept": "audio/*", "Authorization": "Basic SomethingSomethingSomething=="}, 
       text: msgData.message[0].cInfo.text, 
       output: 'output.wav', 
       success: function() { 
        console.log("text to voice complete"); 
        var audio = new Audio('output.wav'); 
        audio.play(); 
       } 
      }); 

ich:

fehlgeschlagen https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize laden: Anfrage-Header-Feld Berechtigung ist nicht erlaubt durch Access-Control-Allow-Headers in Preflight-Antwort.

Bitte beachten Sie, dass ich leicht eine Anfrage erhalten könnte, wie dies von Restlet funktioniert.

Allerdings, wenn ich verwende:

$.ajax({ 
         url: 'https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize', 
         type: 'POST', 
         user: {"something": "something"}, 
         headers: {"Content-Type": "application/json", "Accept": "audio/*"}, 
         data: {"text": msgData.message[0].cInfo.body}, 
         output: 'output.wav', 
         success: function() { 
           console.log("text to voice complete"); 
           var audio = new Audio('output.wav'); 
           audio.play(); 
         } 
       }); 

ich:

stream.watsonplatform.net/text-to-speech/api/v1/synthesize:1 POST https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize 401 (Processed) 
index.html:1 Failed to load https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://52.207.232.200' is therefore not allowed access. The response had HTTP status code 401. 
+0

Siehe https://stackoverflow.com/questions/43105146/how-to-call-ibm-watson-services-from-javascript/43106268#43106268 – sideshowbarker

Antwort

1

Es sieht aus wie IBM Watson Text-To-Speech-Unterstützung macht teilweise CORS (in Ihrem Fall erforderlich). Bitte überprüfen Sie die Antwort: Can't access IBM Watson API locally due to CORS on a Rails/AJAX App

Außerdem finden Sie eine kluge Ratschläge gibt, die Sie informiert nicht Ihre Watson-Anmeldeinformationen in Ihrem JavaScript-Code hinzufügen, und eher Token verwenden: https://console.bluemix.net/docs/services/watson/getting-started-tokens.html#tokens-for-authentication

Wie Sie ‚re auf der Client-Seite arbeiten, vielleicht Watson NPM-Modul oder Bibliotheken ausprobieren (mit Beispiel) wäre eine gute Wahl sein:

https://www.npmjs.com/package/watson-speech

https://watson-speech.mybluemix.net/text-to-speech.html

Hoffe, das hilft!

Verwandte Themen