jQuery弃用了跨域响应函数

时间:2012-03-31 08:44:15

标签: jquery html cross-domain deprecated

基本上我一直在使用这个函数来实现跨域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 个答案:

答案 0 :(得分:3)

我和你有同样的问题并找到了解决方案。虽然事件已经发生,但问题被问到后我决定发布我的解决方案,如果其他人应该像我一样遇到这个问题。

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');
}