Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get $.ajax's default object

Tags:

jquery

Is it possible to get all the defaults that are associcate with the $.ajax function.

So it would return something like this:

{
    global:true,
    headers:{},
    ifModified:false,
    type:"GET",
    url:"the current page url",
    etc....
}
like image 323
locrizak Avatar asked Jul 28 '11 18:07

locrizak


People also ask

What is default datatype in AJAX call?

When sending data to the server, use this content type. Default is "application/x-www-form-urlencoded; charset=UTF-8", which is fine for most cases. If you explicitly pass in a content-type to $.ajax() , then it is always sent to the server (even if no data is sent).

Which object can be used to retrieve data in AJAX?

XMLHttpRequest (XHR) objects are used to interact with servers. You can retrieve data from a URL without having to do a full page refresh. This enables a Web page to update just part of a page without disrupting what the user is doing. XMLHttpRequest is used heavily in AJAX programming.

What triggers AJAX success?

AJAX success is a global event. Global events are triggered on the document to call any handlers who may be listening. The ajaxSuccess event is only called if the request is successful. It is essentially a type function that's called when a request proceeds.

What type of request does AJAX send to the server by default?

By default ajax() method performs http GET request if option parameter does not include method option. The second parameter is options parameter in JSON format where we have specified callback function that will be executed when request succeeds.


1 Answers

From looking at the source code, I believe the (current) defaults are found in jQuery.ajaxSettings, of course also available as $.ajaxSettings. So if you haven't changed them, you should be able to get them from there.

Note that if you have changed them, for example using the $.ajaxSetup utility method, you'll get the new defaults you created, not the inherent ones from the jQuery library.

Also looking at the source code, it seems the defaults are the following:

ajaxSettings: {
    url: ajaxLocation,
    isLocal: rlocalProtocol.test( ajaxLocParts[ 1 ] ),
    global: true,
    type: "GET",
    contentType: "application/x-www-form-urlencoded",
    processData: true,
    async: true,
    /*
    timeout: 0,
    data: null,
    dataType: null,
    username: null,
    password: null,
    cache: null,
    traditional: false,
    headers: {},
    */

    accepts: {
        xml: "application/xml, text/xml",
        html: "text/html",
        text: "text/plain",
        json: "application/json, text/javascript",
        "*": "*/*"
    },

    contents: {
        xml: /xml/,
        html: /html/,
        json: /json/
    },

    responseFields: {
        xml: "responseXML",
        text: "responseText"
    },

    // List of data converters
    // 1) key format is "source_type destination_type" (a single space in-between)
    // 2) the catchall symbol "*" can be used for source_type
    converters: {

        // Convert anything to text
        "* text": window.String,

        // Text to html (true = no transformation)
        "text html": true,

        // Evaluate text as a json expression
        "text json": jQuery.parseJSON,

        // Parse text as xml
        "text xml": jQuery.parseXML
    }
},
like image 171
Tomas Aschan Avatar answered Oct 23 '22 13:10

Tomas Aschan