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

私は以下を試しました。また、getTitle(){ここにあるajaxコードとreturntitle;}などの関数でajaxをラップしようとしました。

(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 に答える