这是我使用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
模式,但无济于事。
有谁知道会发生什么事?
预先感谢您。
+1为通常情况下,Internet Explorer(我的情况下9)似乎是问题_ – Barun
请添加一个链接到工作代码,所以我们可以看看,而不必自己设置一切,谢谢 –
@EmmanuelBucur所以没有数据被传递给spitResult()或success()处理程序? –