1
(function($)
{

    $.fn.makeAjaxPostCall = function(options)
        {
            var settings = $.extend( true,{}, $.fn.makeAjaxPostCall.defaults, options );
            $.ajax({
                type: 'POST',
                url:  settings.url,
                data:   settings.data,
                success: function(data, textStatus, xhr){
                    if (xhr.readyState === 4 && xhr.status === 0) {

                        alert('Network Error');
                    }
                    else {alert(data);
                        settings.callback.call(data);
                    }
                },
                error: function(xhr, textStatus, errorThrown) {
                    if (textStatus !== null) {
                        alert("error: " + textStatus);
                    } else if (errorThrown !== null) {
                        alert("exception: " + errorThrown.message);
                    }
                    else {
                        alert ("error");
                    }
                }
            });
        };
        $.fn.makeAjaxPostCall.defaults = {
            type : 'POST',
            url : '',
            data : {},
            obj : $(document),
            callback : function(data){}
        };

})(jQuery);

This is my ajax function. And I am calling this function as

$(document).makeAjaxPostCall({
    type : 'POST',
    url : 'UpdateNodeStatus',
    data : {
        workshopid : 1,
        userid : 1001,
        level : 5,
        nodeid : 10,
        resolutionid : 1
    },
    callback : function(data){
        alert(data);
    }
});

after post happened I am returning 'true' from servlet.

response.setContentType("text");
    PrintWriter pw = response.getWriter();
    pw.print(true);

I am getting data as 'true' in 'success', but as 'undefined' in callback.

How do I pass 'data' from 'success' to 'callback' function to use it in there?

4
  • 2
    Should it not just be settings.callback(data);? Commented Sep 20, 2013 at 11:56
  • @Archer - using call() should work as well, but all of this looks like just jumping through a lot of hoops to create a new ajax function that does just about the same as the one already available in jQuery ? Commented Sep 20, 2013 at 11:59
  • @adeneo I totally agree. Why extend something that works perfectly anyway? Commented Sep 20, 2013 at 12:02
  • @adeneo - yes I agree, but I don't want to write success and failure code every time I make ajax call,nearly 30 to 40 times in my application in different pages. Commented Sep 20, 2013 at 12:07

1 Answer 1

1

You can direclty call the callback function without .call like following

settings.callback(data);

Or If you want to use the .call then send this as first argument to .call.

if (xhr.readyState === 4 && xhr.status === 0) {
   alert('Network Error');
}
else {
    alert(data);
    settings.callback.call(this, data);
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call

Sign up to request clarification or add additional context in comments.

Comments

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.