Устаревшая функция jQuery для междоменного ответа - PullRequest
0 голосов
/ 31 марта 2012

В основном я использовал эту функцию для достижения междоменного JSONP, но часть функции _success.call(this, { responseText: data.results[0].replace(/<script[^>]+?\/>|<script(.|\s)*?\/script>/gi, '')}, 'success'); завершается с ошибкой Uncaught TypeError: Object #<Object> has no method 'isResolved'

Теперь я знаю, .isResolved() http://api.jquery.com/deferred.isResolved/ устарела, поэтомуМне было интересно, как я смогу заставить его работать с deffered.state() http://api.jquery.com/deferred.state/, который вступил во владение.

Любая помощь будет высоко цениться.Возвращение к предыдущей версии jQuery на самом деле не вариант.

Полная функция ниже.

jQuery.ajax = (function(_ajax){

    var protocol = location.protocol,
        hostname = location.hostname,
        exRegex = RegExp(protocol + '//' + hostname),
        YQL = 'http' + (/^https/.test(protocol)?'s':'') + '://query.yahooapis.com/v1/public/yql?callback=?',
        query = 'select * from html where url="{URL}" and xpath="*"';

    function isExternal(url) {
        return !exRegex.test(url) && /:\/\//.test(url);
    }

    return function(o) {

        var url = o.url;

        if ( /get/i.test(o.type) && !/json/i.test(o.dataType) && isExternal(url) ) {

            // Manipulate options so that JSONP-x request is made to YQL

            o.url = YQL;
            o.dataType = 'json';

            o.data = {
                q: query.replace(
                    '{URL}',
                    url + (o.data ?
                        (/\?/.test(url) ? '&' : '?') + jQuery.param(o.data)
                    : '')
                ),
                format: 'xml'
            };

            // Since it's a JSONP request
            // complete === success
            if (!o.success && o.complete) {
                o.success = o.complete;
                delete o.complete;
            }

            o.success = (function(_success){
                return function(data) {
                    if (_success) {
                        // Fake XHR callback.
                        _success.call(this, {
                            responseText: data.results[0]
                                // YQL screws with <script>s
                                // Get rid of them
                                .replace(/<script[^>]+?\/>|<script(.|\s)*?\/script>/gi, '')
                        }, 'success');
                    }

                };
            })(o.success);

        }

        return _ajax.apply(this, arguments);

    };

})(jQuery.ajax);

1 Ответ

3 голосов
/ 12 июля 2012

У меня была такая же проблема, как и у вас, и я нашел решение. Несмотря на то, что прошло некоторое время с тех пор, как были заданы вопросы, я решил опубликовать свое решение, если кто-то еще столкнется с этим вопросом, как я.

В jquery.xdomainajax.js перейти к строке 62 и изменить

if (_success) {
    // Fake XHR callback.
    _success.call(this, {
        responseText: (data.results[0] || '')
            // YQL screws with <script>s
            // Get rid of them
            .replace(/<script[^>]+?\/>|<script(.|\s)*?\/script>/gi, '')
    }, 'success');
}

до

if (_success) {
    // Fake XHR callback.
    var obj = {
        responseText: (data.results[0] || '')
            // YQL screws with <script>s
            // Get rid of them
            .replace(/<script[^>]+?\/>|<script(.|\s)*?\/script>/gi, '')
    };  
    $.extend(obj,{
        isResolved: function() { return true; },
        done: function() { return true; }
    });

    _success.call(this, obj, 'success');
}
...