Method POST, Status (canceled) error message

前端 未结 6 1488
北恋
北恋 2021-02-20 04:08

I have the following code which is giving me a Method POST, Status (canceled) error message:

$(document).ready(function() {
    var xhr = false;

           


        
相关标签:
6条回答
  • 2021-02-20 04:30

    That is because you are calling abort method which possibly triggers the error handler with appropriate error message.

    You can possibly wait for previous ajax request to complete before making the next call.

    0 讨论(0)
  • 2021-02-20 04:31

    You are using the keyup event, which seems to be the problem.

    If anything at all, you need to wait after typing one character before taking action.

    A better solution might be to follow the same strategy as the JQuery AutoComplete COmponent.

    0 讨论(0)
  • 2021-02-20 04:39

    Ajax is an async type, its not recommonded that u to send request on every keyup event, try the...

    async: false
    

    in post method... it'll pause the subsequent posts until the current request done its callback

    0 讨论(0)
  • 2021-02-20 04:40

    In order to both fix your problem and save on the amount of Ajax calls I have written the following example. This example allows you to handle the following two situations:

    Situation 1:

    The user types slow enough (lets say about one key every 200+ miliseconds
    

    Situation 2:

    The user types fast (my average is about 20 to 50 miliseconds per key)
    

    In the following example there is no need to abort or ignore Ajax calls, you are not spamming Ajax calls and you are using an Object to handle your job. (I even jsFiddled it for you)

    var Handler = {
    
        /**
         * Time in ms from the last event
         */
        lastEvent: 0,
    
        /**
         * The last keystroke must be at least this amount of ms ago
         * to allow our ajax call to run
         */
        cooldownPeriod: 200,
    
        /**
         * This is our timer
         */
        timer: null,
    
        /**
         * This should run when the keyup event is triggered
         */
        up: function( event )
        {
            var d = new Date(),
                now = d.getTime();
    
            if( ( now - Handler.lastEvent ) < Handler.cooldownPeriod ) {
               // We do not want to run the Ajax call
                // We (re)set our timer
                Handler.setTimer();
            } else {
                // We do not care about our timer and just do the Ajax call
                Handler.resetTimer();
                Handler.ajaxCall();
            }
    
            Handler.lastEvent = now;
        },
    
        /**
         * Function for setting our timer
         */
        setTimer: function()
        {
            this.resetTimer();
            this.timer = setTimeout( function(){ Handler.ajaxCall() }, this.cooldownPeriod );
        },
    
        /**
         * Function for resetting our timer
         */
        resetTimer: function()
        {
            clearTimeout( this.timer );
        },
    
        /**
         * The ajax call
         */
        ajaxCall: function()
        {
            // do ajax call
        }
    
    };
    
    jQuery( function(){
    
        var field = jQuery( '#field' );
    
        field.on( 'keyup', Handler.up );
    
    });
    

    Hope this helps.

    0 讨论(0)
  • 2021-02-20 04:40

    Realistically you need a setTimeout method in order to prevent redundant ajax calls being fired.

    clearTimeout(timer);
    
    if($("#txt1").val().length >= 2){
        timer = setTimeout(function(){
            get_data($("#txt1").val());
        }, 400);
    }else{
        get_default();
    }
    

    This should eradicate your problem.

    0 讨论(0)
  • 2021-02-20 04:43

    I suppose that the problem is very easy. If you call xhr.abort(); then the error callback of $.ajax will be called for the pending request. So you should just ignore such case inside of error callback. So the error handler can be modified to

    error: function(jqXHR, textStatus, errorThrown) {
        var err;
        if (textStatus !== "abort" && errorThrown !== "abort") {
            try {
                err = $.parseJSON(jqXHR.responseText);
                alert(err.Message);
            } catch(e) {
                alert("ERROR:\n" + jqXHR.responseText);
            }
        }
        // aborted requests should be just ignored and no error message be displayed
    }
    

    P.S. Probably another my old answer on the close problem could also interesting for you.

    0 讨论(0)
提交回复
热议问题