2013-11-14 58 views
7

这是我使用YQL进行Google翻译的一个类。jQuery ajax请求,承诺不能在IE9中工作

var Translator = { 
    source: 'ro', // default 
    target: 'en', // default 
    url: 'http://query.yahooapis.com/v1/public/yql?q=select * from google.translate where q="', 
    urlRemaining: '";&format=json&diagnostics=true&env=store://datatables.org/alltableswithkeys&callback=', 
    diacritics: Array(), 
    newCharacters: Array(), 

    replaceAll: function(string, replace, replaceWith) { 
     return string.replace(new RegExp(replace, 'g'), replaceWith); 
    }, 

    replaceDiacritics: function(text) { 
     string = text; 

     // diacritics and newCharacters should be arrays of the same length 
     // diacritics should only be specified in lowercase - uppercased version will be assumed 
     // durring the process 
     for (i = 0; i < this.diacritics.length; i++) { 
      string = this.replaceAll(string, this.diacritics[i], this.newCharacters[i]); 
      string = this.replaceAll(string, this.diacritics[i].toUpperCase(), this.newCharacters[i].toUpperCase()); 
     } 

     return string; 
    }, 

    translate: function(text, target, source) { 
     target = target || this.target; 
     source = source || this.source; 

     return $.ajax({ 
      url: this.url + encodeURIComponent(this.replaceDiacritics(text)) + '" and source="' + source + '" and target="' + target + this.urlRemaining, 
      dataType: 'json', 
      cache: false 
     }); 
    }, 

    spitResult: function(x, container) { 
     x.success(function(realData) { 
      $report = realData.query.results.json.sentences; 
      $result = ''; 
      if ($.isArray($report)) { 
       for (i = 0; i < $report.length; i++) { 
        $result += $report[i].trans; 
       } 
      } else { 
       $result = $report.trans; 
      } 

      if (container instanceof jQuery) { 
       container.html($result); 
      } else { 
       container.innerHTML = $result; 
      } 
     }); 
    } 
} 

现在我称它在一组元素在页面

promises = Array(); 

Translator.diacritics = Array('ă', 'â', 'î', 'ș', 'ț'); 
Translator.newCharacters = Array('a', 'a', 'i', 's', 't'); 

$('.translate').each(function() { 
    $this = $(this); 
    promises[promises.length] = Translator.translate($this.html(), 'en', 'ro'); 
    Translator.spitResult(promises[promises.length-1], $this); 
}); 

这正与Firefox和Chrome没有问题。然而,像往常一样,Internet Explorer(我的情况是9)似乎是问题所在。从我已经能够推断出它驻留在诺言解析器(Translate.spitResult) - 这被称为,但没有数据似乎被传递给它。我在控制台中看着它。该承诺数组元素填充了3个对象(我敢肯定是正常的),但它是:

readyState: 0 
responseJSON: undefined, status: 0 
statusText: "No Transfer". 

我试图消除变音符号的功能(现在我不知道是什么原因,因为那里本来应该反正无论如何),我也尝试在ajax调用cache: false模式,但无济于事。

有谁知道会发生什么事?

预先感谢您。

+5

+1为通常情况下,Internet Explorer(我的情况下9)似乎是问题_ – Barun

+1

请添加一个链接到工作代码,所以我们可以看看,而不必自己设置一切,谢谢 –

+0

@EmmanuelBucur所以没有数据被传递给spitResult()或success()处理程序? –

回答

0

是Internet Explorer的是你的问题...... 检查http://caniuse.com/#search=promise

我想你可以使用填充工具(https://github.com/taylorhakes/promise-polyfill)如果是这样的问题,从来没有尝试过的承诺,一个填充工具,但它会像肯定魅力

+0

请花一分钟来解释您发布的链接是参考的。答案应该能够不依靠外部来源而生活。 –

+1

也许是一个错误的问题,一会儿我明白问题在于IE9不支持承诺,所以我链接了承诺支持和polyfill,以便在不支持它的浏览器上使用承诺。但我可能是错的,因为我现在没有看到他使用承诺。 –