这是我制作的一个使用YQL进行Google翻译的课程.
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)似乎是个问题.从我能够推断出它来自它的promise解析器(Translate.spitResult) – 它被调用,但似乎没有数据传递给它.我在控制台里看着它. promise数组元素填充了3个对象(我确信这是正常的),但它是:
readyState: 0 responseJSON: undefined,status: 0 statusText: "No Transfer".
我尝试删除变音符号功能(现在我不完全确定原因,因为无论如何都应该有响应),我也尝试在ajax调用上缓存:false模式,但无济于事.
有谁知道可能是什么问题?
先感谢您.
解决方法
是的Internet Explorer是你的问题……
检查 http://caniuse.com/#search=promise
检查 http://caniuse.com/#search=promise
我认为你可以使用polyfill(https://github.com/taylorhakes/promise-polyfill),如果这是问题,从来没有试过polyfill的承诺,但它会像一个魅力肯定的工作