Bhavik Patel
Bhavik Patel

Reputation: 789

update or extend $.ajax jQuery

i want to update $.ajax method of jQuery so it have default timeout of 20 seconds and upon timeout i want to call my own function and i also want to create network error function.

is this possible ?

i want to do this because i don't want add timeout in all my $.ajax request.

Please ask query if you have any

like this for every function it's require me to add timeout.

$.ajax(
                        {
                            url: "getDuctions.php",
                            type: "POST",
                            data: {dept_cd: dept, filter: "payroll_struct_empl_map", primary: "empl_cd"},
                            beforeSend: function() {
                                $("#duct").html("<option value=''>Loading</option>");
                            },
                            success: function(data) {
                                $("#duct").html(data);
                            },timeout:20000,
                        }
                );

Upvotes: 0

Views: 926

Answers (2)

Abhilash
Abhilash

Reputation: 1610

Default options can be declared using $.ajaxSetup()

Update: you can also have default functions in ajaxSetup

$.ajaxSetup({
    timeout:20000,
    error: function(jqXHR, textStatus, errorThrown){
        if( textStatus == 'timeout'){
            // Write timeout callback function here...
        }
    }
});

Upvotes: 2

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100195

you could do:

$.ajax({
 complete: callbackFunction, 
 url: 'some_url', 
 type: "POST",
 ......
 timeout: 20000, 
 error: errorCallback
});

or

$.ajaxSetup({timeout: 20000, error: errorCallback});

The error function is called when it fails such as 404 errors, or when the timeout is hit.

Did you mean something like that..

Upvotes: 1

Related Questions