Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Sep 2013
    Posts
    20
    Thanks
    4
    Thanked 0 Times in 0 Posts

    When are jquery ajax request guaranteed to succeed?

    Hi,
    I am wondering under what conditions an ajax post request may fail.

    Lets assume the server is always up. Can I then also assume that when I do the HTTP post request with jquery I will always get back the output produced by the server? In other words, does the code below always (ie. without exceptions) run either the onSucess function or the onFailure function? And if not, when can it fail to do so?

    I use the following code to send a request to the server to store a player's chess move in a database:
    Code:
    ChessNetworkCommunicator.prototype.makeMove = function( gameId, moveNumber, originX, originY, destX, destY, promotionChoice, onSuccess, onFailure ){
    	var postvars = this.QS_COMMAND + "=" + this.CMD_MAKE_MOVE +
    					'&' + this.QS_GAME_ID + '=' + gameId +
    				   	'&' + this.QS_MOVE_NUMBER + '=' + moveNumber +
    				   	'&' + this.QS_ORIGIN_X + '=' + originX +
    				   	'&' + this.QS_ORIGIN_Y + '=' + originY +
    				   	'&' + this.QS_DESTINATION_X + '=' + destX +
    				   	'&' + this.QS_DESTINATION_Y + '=' + destY +
    					'&' + this.QS_PROMOTION_PIECE + '=' + promotionChoice ;
    	
    	$.post( this.URL_CHESS_SERVER, postvars, $.proxy( function( jsonServerResponse ){
    		if( jsonServerResponse.meta == this.META_SUCCESSFUL ){
    			onSuccess();
    		} else{
    			onFailure();
    		}
    	}, this ) );
    }
    Grtz. Stefan

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,391
    Thanks
    11
    Thanked 568 Times in 561 Posts
    What you have here is more of a functional exception than a technical exception. You will only get functional exceptions if the server is up and running and generates them inside the response.

    Your response is a JSON object. As soon as you get this object you know that the server is up and running. The field "meta" inside the response contains the functional exception as some kind of message on which you react in your code with the if/else construct.

    A technical exception would be thrown if there is any technical error (i.e. connection problems or similar). In that case you won't receive the JSON response and you won't reach either the onSuccess or the onFailure methods


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •