2

小提琴:http: //jsfiddle.net/gpTpK/

我遇到的问题是执行 $.ajax 时标题变量没有更新/更改,我知道 ajax 调用正在工作,因为我尝试替换该行

title = $(xml).find("title").text();

console.log($(xml).find("title").text());

果然它确实返回了标题但是当使用原始行时变量标题不会改变

我已经尝试过,它确实可以将 ajax 调用放在外面 (function($){})(jQuery);

(function($) {
    $.fn.getPost = function(options) {
        var $this = $(this);
        var defaults = {
            method: "html",
            blogID: "",
            postID: "",
            done: null
        };
        var options = $.extend(defaults, options);
        var title;
        $.ajax({
            type: "GET",
            url: "http://www.blogger.com/feeds/724793682641096478/posts/default/3551136550258768001",
            dataType: "xml",
            dataType: 'jsonp',
            success: function(xml) {
                title = $(xml).find("title").text();
            }
        });
        return $this.each(function() {
            if (options.done) {
                options.done.call(undefined, title);
            }
        });
    };
})(jQuery);

我已经尝试了以下方法,我还尝试将 ajax 包装在一个函数中,例如 getTitle(){ajax code here with return title;}

(function($) {
    $.fn.getPost = function(options) {
        var $this = $(this);
        var defaults = {
            method: "html",
            blogID: "",
            postID: "",
            done: null
        };
        var options = $.extend(defaults, options);
        var title;
        getAjax();
        return $this.each(function() {
            if (options.done) {
                options.done.call(undefined, title);
            }
        });

        function getAjax() {
            $.ajax({
                type: "GET",
                url: "http://www.blogger.com/feeds/724793682641096478/posts/default/3551136550258768001",
                dataType: "xml",
                dataType: 'jsonp',
                async: false,
                success: function(xml) {
                    title = $(xml).find("title").text();
                }
            });
        }
    };
})(jQuery);
4

1 回答 1

1

抱歉,我花了很长时间试图弄清楚(我不是因为懒惰而问:P),不管这里是那些感兴趣的人的解决方案:)

(function($) {
    $.fn.getPost = function(options) {
        var $this = $(this);
        var defaults = {
            method: "html",
            done: null
        };
        var options = $.extend(defaults, options);
        var title;
        var sorf;
        $.ajax({
            type: "GET",
            url: "http://www.blogger.com/feeds/724793682641096478/posts/default/3551136550258768001",
            dataType: "xml",
            dataType: 'jsonp',
            success: function(xml) {
                title = $(xml).find("title").text();
                sorf = 1;
            },
            error: function(){
                sorf = 0;
            },
            complete: function() {
                returnvals(sorf);
            }
        });

        function returnvals(sorf) {
         if(sorf){
         //success
            return $this.each(function() {
                if (options.done) {
                    options.done.call(undefined, title);
                }
            });
         }else{// failure}
        }
    };
})(jQuery);
于 2012-11-24T15:30:53.623 回答