Search code examples
jqueryazurelanguage-translationmicrosoft-translator

Microsoft Azure Translator AJAX API not working


I am trying to convert from the current version of MS Bing Translator to the new Azure one.

I created an access token as described in the new documentation and although the following example (for Azure) supplied by Microsoft works correctly:

function translate() {

  var from = "en", to = "es", text = "hello world";
  var s = document.createElement("script");
  s.src = "http://api.microsofttranslator.com/V2/Ajax.svc/Translate" +
            "?appId=" + settings.appID +
            "&from=" + encodeURIComponent(from) +
            "&to=" + encodeURIComponent(to) +
            "&text=" + encodeURIComponent(text) +
            "&oncomplete=mycallback";
  document.body.appendChild(s);
}

function mycallback(response) {
  alert(response); 
}

I would like to convert the above code to a jQuery call.

I modified a similar jQuery ajax call from the previous version which worked, but a parseerror-jQuery17206897480448242277_1343343577741 was not called is issued:

  function jqueryTranslate() {
    var p = {};
    p.appid = settings.appID;
    p.to = "es";
    p.from = "en";
    p.text = "Goodbye Cruel World";
    p.contentType = 'text/html';
    $.ajax({
      url: 'http://api.microsofttranslator.com/V2/Ajax.svc/Translate',
      data: p,
      dataType: 'jsonp',
      jsonp: 'oncomplete',
      complete: function (request, status) {
      },
      success: function (result, status) {
        alert(result);
      },
      error: function (a, b, c) {
        alert(b + '-' + c);
      }
    });
  }

I would very much appreciate and understanding of what is going wrong, so TIA for your time.


Solution

  • The other issue is that the Bing AppID mechanism for authenticating against translator has been deprecated.

    Microsoft have a blog post detailing the process for getting access to Translator in the Windows Azure Marketplace here:

    http://blogs.msdn.com/b/translation/p/gettingstarted1.aspx

    There's an example in ASP.NET here: http://blogs.msdn.com/b/translation/p/gettingstarted2.aspx

    The recommendation is (at least) to put your code for getting your token server side in ASP.NET,PHP,Node or something similar so that you Client ID and Client Secret aren't exposed.

    Once you've gotten the access token, it needs to be written into the HTTP headers of the call to the service. The ASP.NET sample shows that, and it should be relatively easy to adapt to JQuery.