简体   繁体   中英

.load() works only first time but .ajax() works fine every time

The problem is because of slow network connections due to long distance access. The jQuery .load() function doesn't seems to load the HTML page from second time and browser had or the request in about everytime. 了请求。 The same page when loaded using .ajax() is not causing a problem. Why jQuery .load() is been repeatedly aborted by the browser from second time? The response JSP page does contain several .ajax() requests. Is this causing problem from second time?

You can use ajaxSetup() before calling load() to set timeout

$.ajaxSetup({
    timeout: 30000
});

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

Please check timeout parameter

Referance

Demo

$.ajax({
    url: "/test_ajax_timeout/",
    type: "GET",
    dataType: "json",
    timeout: 15,
    success: function(response) { alert(response); },
    error: function(x, t, m) {
        if(t==="timeout") {
            alert("got timeout");
        } else {
            alert(t);
        }
    }
});

If you changed timeout above a limit you will see error. You can set up an error callback as showed below

$.ajax({}).error(function(){ // Code });

I got the solution by using .ajaxSetup() before calling .load() as recommended by @Chang

But I would like to use .ajax() call with some desired timeout instead of using .load() because the .ajaxSetup() is not recommended to use since it is used to configure default ajax options.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM