jQuery ajax请求,承诺不在IE9中工作

Ema*_*cur 7 javascript ajax jquery promise internet-explorer-9

这是我制作的一个使用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;
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我在页面中的一组元素上调用它

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);
});
Run Code Online (Sandbox Code Playgroud)

这对Firefox和Chrome来说没有问题.但是,像往常一样,Internet Explorer(在我的情况下为9)似乎是个问题.从我能够推断出它来自promise resolver(Translate.spitResult) - 它被调用,但似乎没有数据传递给它.我在控制台里看着它.promise数组元素填充了3个对象(我确信这是正常的),但它是:

readyState: 0
responseJSON: undefined, status: 0
statusText: "No Transfer".
Run Code Online (Sandbox Code Playgroud)

我尝试删除变音符功能(现在我不确定为什么,因为无论如何都应该有响应),我也尝试cache: false了ajax调用模式,但无济于事.

有谁知道可能是什么问题?

先感谢您.

小智 0

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

我认为你可以使用一个polyfill(https://github.com/taylorhakes/promise-polyfill)如果这就是问题所在,从来没有尝试过使用polyfill来实现promise,但它肯定会像一个魅力一样工作