Đây là lớp tôi đã sử dụng YQL để thực hiện Google Dịch.Yêu cầu jQuery ajax với lời hứa không hoạt động trong 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;
}
});
}
}
Và bây giờ tôi gọi đó là trên một tập hợp của các yếu tố trong trang
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);
});
này đang làm việc không có vấn đề với Firefox và Chrome. Tuy nhiên, như thường lệ, Internet Explorer (9 trong trường hợp của tôi) có vẻ là vấn đề. Từ những gì tôi đã có thể suy ra nó nằm trong trình phân giải lời hứa (Translate.spitResult
) - được gọi, nhưng không có dữ liệu nào dường như được truyền cho nó. Tôi nhìn nó trong bảng điều khiển. Phần tử mảng lời hứa được điền bằng 3 đối tượng (mà tôi chắc chắn là bình thường), nhưng nó là:
readyState: 0
responseJSON: undefined, status: 0
statusText: "No Transfer".
Tôi đã cố gắng loại bỏ hàm dấu phụ (bây giờ tôi không chắc chắn lý do tại sao, vì có lẽ đã có một phản ứng anyway), tôi cũng đã thử chế độ cache: false
trong cuộc gọi ajax, nhưng không có kết quả.
Có ai biết điều gì có thể là vấn đề không?
Cảm ơn bạn trước.
+1 cho bình thường, Internet Explorer (9 trong trường hợp của tôi) có vẻ là vấn đề_ – Barun
vui lòng thêm liên kết vào mã làm việc để chúng tôi có thể xem mà không cần phải tự thiết lập mọi thứ, cảm ơn –
@EmmanuelBucur. dữ liệu được truyền cho trình xử lý spitResult() hoặc thành công()? –