-1

I have 2 AJAX calls that achieve the same result but they are written differently. I just wanted to ask if there is any real benifit it either one or if basically they are the same:

$('#joinCaptchaTextBox').keyup(function() {
    $.get('scripts/ajax/script.php', {
        'join_captcha': '1',
        'captcha': $('#joinCaptchaTextBox').val()},         
        function(data) {
            var obj = JSON.parse(data);
            if(obj.ajaxResponse.status) {
                $('#joinCaptchaNotAcceptable').hide();
                $('#joinCaptchaAcceptable').show();
            }else{
                $('#joinCaptchaAcceptable').hide();
                $('#joinCaptchaNotAcceptable').show();
            }
    });
});

And

$('#joinCaptchaTextBox').on('keyup', function() {
            var data = {
                    join_captcha: '1',
                    captcha : $('#joinCaptchaTextBox').val()
            };

            $.ajax({
                url : 'scripts/ajax/script.php',
                data: data,
                dataType: 'json'
            }).done(function(result) {
                $('#joinCaptchaNotAcceptable').toggle(!result.ajaxResponse.status);
                $('#joinCaptchaAcceptable').toggle(result.ajaxResponse.status);
            });
});

I can see the second one is a little more efficent with the toggle but outside of this is either one specifically better? Just trying to understand which type to use. Note: a GET HTTP request is required.

thankyou

user1105192
  • 402
  • 5
  • 16
  • 2
    functionally, they are both identical. I prefer the second because i prefer being more specific with my code. $.ajax allows you to specify the options directly, where as $.get does it for you. – Kevin B Aug 06 '13 at 22:00

3 Answers3

1

$.get is shorthand for $.ajax, so they are doing the same thing. See the jQuery documentation.

ShadowCat7
  • 804
  • 8
  • 16
1

ajax includes two methods which are post and get , u can define them like below. $.post (or $.get) is part of ajax ,leight-weight than ajax. only include url , data , success etc methods,properties .

 $.ajax({
    type: "POST",
    url: "modplatform.php",
    data: "MPAx=index&proc=home::send_feedback&" + $('#feedback_send').serialize(),
    success: function (msg) {

        parent.$.fancybox.close();

        if (msg == '1') {

            MPAlert(msg, 'i');
        } else {

            MPAlert(msg, 'i');
        }

    }
});

}

Ajax has beforesend afterSend , successCode , jsonCallback etc prop and method. $.post and $.get is light weight of ajax. The same work they will do , which is up to you

Emre Karataşoğlu
  • 1,649
  • 1
  • 16
  • 25
0

Here is a topic that could interest you :

Is $.get of jquery asynchronous?


I was wondering if both were asynchronous. It seems like they both express the same thing.

Community
  • 1
  • 1
Boubou
  • 632
  • 5
  • 18