开发者

"ajax" call in jQuery fires "success" function even when server does not respond

开发者 https://www.devze.com 2023-01-20 15:55 出处:网络
It looks li开发者_如何学Cke jQuery\'s .ajax function fires the \'success\' function even when it gets no response from the server.I\'d consider this an error.I detected this event by checking to see i

It looks li开发者_如何学Cke jQuery's .ajax function fires the 'success' function even when it gets no response from the server. I'd consider this an error. I detected this event by checking to see if the 'request.status===0', are there any other instances that I should check for where the 'success' function will be fired even if there is an error situation?

I've posted the code I'm using now below. How is this type of situation normally handled?

$.ajax({ 
    async : true, cache : false, dataType : 'json',
    type : 'POST',
    url : 'person/' + personKey,
    data : JSON.stringify({firstName:'Jilopi',}),

    success : function(data, textStatus, request){
        if( request.status === 0 ){
            alert('Error: problem saving person - no response');
        }else{
            alert('Success: saved person');
        }
    },
    error : function(request, textStatus, errorThrown){
        alert('Error: problem saving person');
    },
});


You've identified the only "false success" issue I know of. To elaborate on it a bit: this was a work-around in place strictly for Opera (which returns what's actually a 304 as a 0), you can see the comment in 1.4.2 (and prior versions) here: http://github.com/jquery/jquery/blob/1.4.2/src/ajax.js#L551

However, it has been removed in jQuery 1.4.3 because of your exact issue, you can see the change that did it here.

Short answer: upgrade to jQuery 1.4.3+ to resolve your issue and you can do away with your status === 0 check.


I also had some need to show if the ES Server is online or not! My Solution in words:

I used long polling to check my server status! And ES Already have a REST API Call just to get the information about the ES Server!

That's Cluster Health request!

Read about ES Cluster Health Requests here!

Just type the following on your Web Browser and see the response of it about your ES Server!

http://your-server-url:9200/_cluster/health

Simple jQuery long-polling recursive function snippet:

(function poll(){
health_url = "http://your-server-url:9200/_cluster/health";
online_ajax = false;
setTimeout(function(){
    $.ajax({
     url: health_url,
     dataType: "json",
     success: function(data){
            if (data.status == 'yellow') {
                // Show Server-Online Status Message in your App
            } else {
                // Show Server-Offline Status Message in your App
            };
            //Setup the next poll recursively
            poll();
          }
      })
      .fail(function(){
            // Show Server-Offline Status Message in your App
      });
}, online_status_time_out);
})();

Hope this answer is helpful to everyone!

Cheers!!!

0

精彩评论

暂无评论...
验证码 换一张
取 消