Razort4x
Razort4x

Reputation: 3406

Where is jQuery get?

I was using getJSON but it didn't worked the way I expected, the WebService was returning xml even thought specifically specified as ResponseFormat = ResponseFormat.JSON. After reading some post(s) on the internet I found that the problem was getJSON won't set the contentType to application/json; charset=utf8, rather it lets it be default. I verified it by calling $.ajax and setting contentType to application/json; charset=utf8. So I though to look into jQuery-1.9.1.js and find out how getJSON is implemented. Here's the exact signature from the file...

getJSON: function( url, data, callback ) {  // line 8107 as of 1.9.1.js
    return jQuery.get( url, data, callback, "json" );
}

So, I looked further to find how is jQuery.get implemented. But to my utmost surprise I can't find any??? I looked many times, but there is no get defined in the file that takes 4 or more arguments? How could this be? If it is not defined, how is get, and by extension getJSON (and also getScript) are getting called? Can anyone please check and verify this?

ps: please don't tell me I can use ajaxSetup etc. and there are other ways to do it, I know, but what's puzzling my mind is, I couldn't find jQuery.get definition, so how and from where is it getting called?

Upvotes: 0

Views: 56

Answers (2)

Alastair Pitts
Alastair Pitts

Reputation: 19601

Have a look at: https://github.com/jquery/jquery/blob/master/src/ajax.js

Specifically line 197-214.

They pass through get and post to jQuery.ajax()

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

Line 197 of the ajax.js file is where the get and post functions are defined:

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({
            url: url,
            type: method,
            dataType: type,
            data: data,
            success: callback
        });
    };
});

As you can see they are simply calling the jQuery.ajax method.

Upvotes: 2

Related Questions