2

Fiddle: http://jsfiddle.net/gpTpK/

The issue I am having is that the title variable is not updated/changed when the $.ajax is executed, I know that the ajax call is working as I have tried replacing the line

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

with

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

and sure enough it does return the title however when using the orginal line the variable title doesn't change

I have tried and it does work putting the ajax call outside (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);

I have tried the below and i have also tried wrapping the ajax in a function such as 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);
2
  • The return will almost always happen before the AJAX request call the function supplied to success. Commented Nov 24, 2012 at 15:26
  • @nhahtdh not almost ... always Commented Nov 24, 2012 at 15:30

1 Answer 1

1

sorry, I have spent ages trying to figure it (I didn't ask out of laziness :P), regardless here's the solution for those interested :)

(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);
Sign up to request clarification or add additional context in comments.

3 Comments

Why complete? Shouldn't the call be in success? When the request fails, complete will also be called.
oh sorry :/ that's not the whole portion of my plugin, it will pass a parameter when there is an error, my bad should have mentioned that
I don't know, it's your own solution to your problem, so you know it best. I just find it weird, but if it is your requirement then it should be OK.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.