slow jquery ajax response

1,081 views
Skip to first unread message

lilw0

unread,
May 24, 2016, 5:06:09 PM5/24/16
to Bitly API
Hi all, I have a jquery ajax call below to bitly that on occasion takes over 2 minutes to finish when it fails from a proxy error, although the timeout on the POST is set to 3 sec. Also, some of the successful POSTs take over 7 seconds.  Along with timeout I'm using  beforeSend with request abort to stop previous request if a new request shows up. I wonder if there is anything else I could do on my end with ajax to drop a any request, successful or not, if it is taking longer than 3 seconds. Much appreciate your time.


var obj = null;    
getURL = function(post_data, tryPost, id){
    var t3 = new Date().getTime(); 
    console.log("urlSharing ajax start id:", id , "t3:", t3);
    
    obj = $.ajax({
         async:false,
         type:"POST",                 
         url: bitlyUrl,
         data: post_data,
         timeout: requestTimeout,
         beforeSend: function(){           
            if(obj != null){
                obj.abort();
            }
        }         
      })           
    .done(function(data, textStatus, jqXHR){
if(data){
    url_rsp = data.data.url;
    
            var t4 = new Date().getTime(); 
            console.log("urlSharing ajax OK End id:",  id, "t4:", t4, "time took:", t4-t3);
}
    })
    .fail(function(jqXHR, textStatus, errorThrown ){
        var err = jqXHR.statusText;
        url_rsp = null;
        
        var t4 = new Date().getTime(); 
        console.log("urlSharing ajax Fail End id:",  id, "t4:", t4, "time took:", t4-t3);
        
        console.log("ERROR in urlSharing: getURL for ID", id, "post_data:", post_data,  "jqXHR:", jqXHR.statusText, "textStatus:", textStatus);
     
     });
     
    return obj;   
}

Peter Herndon

unread,
May 24, 2016, 5:09:42 PM5/24/16
to Bitly API
Hi there,

I'm not going to be able to suggest improvements to your code, but I will ask to what URL are you POSTing? We have very few endpoints in our public API that accept POSTs, most of them only accept GETs. Perhaps the endpoint is not a normal one, and I can help figure out why it's taking so long.

Regards,

---Peter

Peter Herndon
Sr. Application Engineer
@Bitly

lilw0

unread,
May 24, 2016, 5:27:23 PM5/24/16
to Bitly API
Hi Peter, I'm using this url:
I'm with abc group. 

lilw0

unread,
May 24, 2016, 5:27:23 PM5/24/16
to Bitly API
Hi Peter, I'm using this url:

I'm with abc tech group, if it makes any difference.

Peter Herndon

unread,
May 24, 2016, 5:35:14 PM5/24/16
to Bitly API
Hi there,

As I thought, that API endpoint does not accept POST requests, only GETs. The documentation is available at http://dev.bitly.com/links.html#v3_shorten for the endpoint. Switch to a GET and you should see responses in a much more timely fashion.

Regards,

---Peter

Peter Herndon
Sr. Application Engineer
@Bitly

lilw0

unread,
May 24, 2016, 5:53:46 PM5/24/16
to Bitly API
Sweet, thank you! Let me try that.

lilw0

unread,
May 24, 2016, 7:09:55 PM5/24/16
to Bitly API
I did change the ajax to do GET, but I'm still seeing slow response time - over 150k ms (2.5 min) for proxy errors calls, and 76k ms (1.2 min) for one of the success calls.   

Peter Herndon

unread,
May 25, 2016, 10:53:27 AM5/25/16
to Bitly API
Well, I still can't speak to your JavaScript in detail, though it does look fine to my eyes, but there is something horribly wrong with your connection to our servers. Whether that's a problem in your code, in your network connection, or in something between your system and ours, I don't know. However, I can say that the problem seems specific to you, rather than a general problem. Our servers are not down at the moment (http://status.bitly.com/) and haven't been, and the worst case time to contact our servers is a little bit over 5 seconds, and that's from Nantong, China. Average time world-wide is a bit over 600ms (https://pulse.turbobytes.com/results/5745b605ecbe406ba50014e5/).

You could try removing your code from the critical path, by timing a command-line shorten. That way you will be able to tell if the problem is in your code, or if it is in your network connection to our servers. Here's an example of what that would look like, as run on a Linux, UNIX, or Mac OS X system that has both the 'time' and 'curl' commands installed:

```
{ "status_code": 200, "status_txt": "OK", "data": { "long_url": "http:\/\/google.com\/", "url": "http:\/\/phoukka.org\/1UflLJo", "hash": "1UflLJo", "global_hash": "LmvF", "new_hash": 0 } }

real 0m0.772s
user 0m0.028s
sys 0m0.016s
```

Note that you will need to put your access token into the curl command where I have INSERTYOURACCESSTOKEN above. Give that a try from your production system, if the problem still exists then the issue is somewhere in your network connection to our servers. Note, it may not necessarily be a problem *in your network*, it might be a problem with an intermediary system between your connection and ours. You might be able to find out what that is using the traceroute command. Or, the problem might well be in some part of your network. If, on the other hand, the problem does not exist when running the command-line version, then the issue is somewhere in your code.

Good luck tracking down the issue!

Regards,

---Peter

Peter Herndon
Sr. Application Engineer
@Bitly

lilw0

unread,
May 25, 2016, 2:16:47 PM5/25/16
to Bitly API
Thank you Peter for the numbers, they will help in troubleshooting. I'm almost certain this is network related.  
Reply all
Reply to author
Forward
0 new messages