Microsoft Azure转换程序AJAX API无法正常工作

时间:2012-07-26 22:56:29

标签: jquery azure language-translation microsoft-translator

我正在尝试将当前版本的MS Bing转换器转换为新的Azure转换器。

我创建了一个访问令牌,如新文档中所述,尽管Microsoft提供的以下示例(适用于Azure)可以正常工作:

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); 
}

我想将上面的代码转换为jQuery调用。

我修改了之前版本的类似jQuery ajax调用,但是发布了parseerror-jQuery17206897480448242277_1343343577741 was not called

  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);
      }
    });
  }

我非常感谢和理解出了什么问题,所以TIA适合你的时间。

3 个答案:

答案 0 :(得分:4)

要将Bing Translator与身份验证令牌一起使用,首先需要像PHP脚本token.php这样的服务器端脚本。它将从您网页上的javascript每隔9分钟调用一次。

<?php
$ClientID="your client id";
$ClientSecret="your client secret";

$ClientSecret = urlencode ($ClientSecret);
$ClientID = urlencode($ClientID);

// Get a 10-minute access token for Microsoft Translator API.
$url = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13";
$postParams = "grant_type=client_credentials&client_id=$ClientID&client_secret=$ClientSecret&scope=http://api.microsofttranslator.com";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postParams);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);  
$rsp = curl_exec($ch); 

print $rsp;
?>

然后这个html页面将显示一个从英语翻译成法语的双框界面。

注意:此帖子的早期版本缺少前5行,因此无法加载jQuery。 (抱歉,@ db1。)工作脚本在线:

http://www.johndimm.com/bingtrans/

<html>

<head>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

  <script language="javascript">
    var g_token = '';

    function onLoad() {
      // Get an access token now.  Good for 10 minutes.
      getToken();
      // Get a new one every 9 minutes.
      setInterval(getToken, 9 * 60 * 1000);
    }

    function getToken() {
      var requestStr = "/bingtrans/token.php";

      $.ajax({
        url: requestStr,
        type: "GET",
        cache: true,
        dataType: 'json',
        success: function (data) {
          g_token = data.access_token;
        }
      });
    }

    function translate(text, from, to) {
      var p = new Object;
      p.text = text;
      p.from = from;
      p.to = to;
      p.oncomplete = 'ajaxTranslateCallback'; // <-- a major puzzle solved.  Who would have guessed you register the jsonp callback as oncomplete?
      p.appId = "Bearer " + g_token; // <-- another major puzzle.  Instead of using the header, we stuff the token into the deprecated appId.
      var requestStr = "http://api.microsofttranslator.com/V2/Ajax.svc/Translate";

      window.ajaxTranslateCallback = function (response) {
        // Display translated text in the right textarea.
        $("#target").text(response);
      }

      $.ajax({
        url: requestStr,
        type: "GET",
        data: p,
        dataType: 'jsonp',
        cache: true
      });
    }


    function translateSourceTarget() {
      // Translate the text typed by the user into the left textarea.
      var src = $("#source").val();
      translate(src, "en", "fr");
    }
  </script>
  <style>
    #source,
    #target {
      float: left;
      width: 400px;
      height: 50px;
      padding: 10px;
      margin: 10px;
      border: 1px solid black;
    }
    #translateButton {
      float: left;
      margin: 10px;
      height: 50px;
    }
  </style>
</head>

<body onload="onLoad();">

  <textarea id="source">Text typed here will be translated.</textarea>
  <button id="translateButton" onclick="translateSourceTarget();">Translate English to French</button>
  <textarea id="target"></textarea>

</body>

</html>

答案 1 :(得分:0)

您可以尝试在呼叫中添加 jsonpCallback 并为其定义新功能。当我将jQuery代码与Microsoft的示例进行比较时,这似乎是缺失的。

  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',
      jsonpCallback: 'onCompleteCallback',   <------------------ THIS LINE
      complete: function (request, status) {
      },
      success: function (result, status) {
        alert(result);
      },
      error: function (a, b, c) {
        alert(b + '-' + c);
      }
    });
  }

  function onCompleteCallback(response) {    <------------------- THIS FUNCTION
    alert('callback!'); 
  }

答案 2 :(得分:0)

尝试了John Dimm提交的脚本,但它对我不起作用。返回一个空白框,状态为304 Not Modified。相反,我在msdn博客上使用PHP和Microsoft翻译代码在此链接http://blogs.msdn.com/b/translation/p/phptranslator.aspx,它工作得很漂亮