开发者

Jquery ajax calls, which is best? [closed]

开发者 https://www.devze.com 2023-04-07 07:31 出处:网络
Closed. This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing th
Closed. This question is opinion-based. It is not currently accepting answers.

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 9 years ago.

Improve this question

I have found this information on the web about jquery ajax requests.

load(): Load a piece of html into a container DOM.

$.getJSON(): Load a JSON with G开发者_JS百科ET method.

$.getScript(): Load a JavaScript.

$.get(): Use this if you want to make a GET call and play extensively with the response.

$.post(): Use this if you want to make a POST call and don’t want to load the response to some container DOM.

$.ajax(): Use this if you need to do something when XHR fails, or you need to specify ajax options (e.g. cache: true) on the fly.

But it doesn't say anything about performance which is best for using it for what. Which is best for from validation? Which is best in speed and such alike.

I have never used jQuery ajax before and wondered from some of you more experienced programmers in your time of using jQuery ajax requests which have your find suit your needs best?

What is JSON I know it is called javascript object notation but what does mean?


None will be better or worse. Use what is appropriate for the situation.


Like kingjiv said, each and every one of those go through the $.ajax method.

load() sets the type to get/post does other things in the background to make sure that the response is populated on the selected element then calls $.ajax. Below is the code.

load: function( url, params, callback ) {
    if ( typeof url !== "string" && _load ) {
        return _load.apply( this, arguments );

    // Don't do a request if no elements are being requested
    } else if ( !this.length ) {
        return this;
    }

    var off = url.indexOf( " " );
    if ( off >= 0 ) {
        var selector = url.slice( off, url.length );
        url = url.slice( 0, off );
    }

    // Default to a GET request
    var type = "GET";

    // If the second parameter was provided
    if ( params ) {
        // If it's a function
        if ( jQuery.isFunction( params ) ) {
            // We assume that it's the callback
            callback = params;
            params = undefined;

        // Otherwise, build a param string
        } else if ( typeof params === "object" ) {
            params = jQuery.param( params, jQuery.ajaxSettings.traditional );
            type = "POST";
        }
    } // after this is calls $.ajax and passes in the parameters 

$.getJSON(): This just calls $.get setting the dataType to json. $.get in turn calls $.ajax

getJSON: function( url, data, callback ) {
        return jQuery.get( url, data, callback, "json" );
    }

$.getScript(): You guessed it, just calls $.get and passes in the dataType as script.

getScript: function( url, callback ) {
        return jQuery.get( url, undefined, callback, "script" );
    },

$.get() & $.post(): These two methods call $.ajax as well.

jQuery.each( [ "get", "post" ], function( i, method ) {
    jQuery[ method ] = function( url, data, callback, type ) {
        // shift arguments if data argument was omitted
        if ( jQuery.isFunction( data ) ) {
            type = type || callback;
            callback = data;
            data = undefined;
        }

        return jQuery.ajax({
            type: method,
            url: url,
            data: data,
            success: callback,
            dataType: type
        });
    };
});

In conclusion, use whatever is suited to the purpose, just depends on what you intend to do. The performance should be the same since you have seen they all go through $.ajax


They are all just shorthand for $.ajax() with a certain subset of parameters. None is "better" than the other, whichever works for your situation is "best".


I would suggest $.ajax() for flexibility and it gives you more option..

The $.ajax() function underlies all Ajax requests sent by jQuery. It is often unnecessary to directly call this function, as several higher-level alternatives like $.get() and .load() are available and are easier to use. If less common options are required, though, $.ajax() can be used more flexibly.


If you look in the documentation, get uses ajax under the covers. Also look at this

http://api.jquery.com/jQuery.ajaxSetup/

it could save you some typing.

0

精彩评论

暂无评论...
验证码 换一张
取 消