方法POST,状态(已取消)错误消息

时间:2012-03-29 15:29:08

标签: javascript ajax jquery readystate

我有以下代码,它给出了Method POST, Status (canceled)错误消息:

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

    get_default();

    $('#txt1').keyup( function() {
        if(xhr && xhr.readyState != 4){
            alert("abort");
            xhr.abort();
        }

        if ($("#txt1").val().length >= 2) {
            get_data( $("#txt1").val() );
        } else {
            get_default();
        }
    });

    function get_data( phrase ) {
        xhr = $.ajax({
            type: 'POST',
            url: 'http://intranet/webservices.asmx/GetData',
            data: '{phrase: "' + phrase + '"}',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function( results ) {
                $("#div1").empty();

                if( results.d[0] ) {
                    $.each( results.d, function( index, result ) {
                        $("#div1").append( result.Col1 + ' ' + result.Col2 + '<br />' );
                    });
                } else {
                    alert( "no data available message goes here" );
                }
            },
            error: function(xhr, status, error) {
                 var err = eval("(" + xhr.responseText + ")");
                 alert(err.Message) ;
            }
        });
    }

    function get_default() {
        $('#div1').empty().append("default content goes here.");
    }

});

只要每个ajax请求完成,代码实际上都有效,但如果我快速输入txt1,即在上一个请求完成之前键入下一个字符,我会收到错误消息Method POST, Status (canceled)。 / p>

任何人都知道为什么会这样,以及如何纠正错误?

6 个答案:

答案 0 :(得分:11)

我认为问题很容易。如果您致电xhr.abort();,那么error的{​​{1}}回调将被称为待处理请求的 。所以你应该忽略$.ajax回调中的这种情况。因此error处理程序可以修改为

error

P.S。可能另一个我关于密切问题的old answer也可能对你感兴趣。

答案 1 :(得分:0)

这是因为您正在调用abort方法,该方法可能会触发错误处理程序并显示相应的错误消息。

您可以在下一次通话之前等待之前的ajax请求完成。

答案 2 :(得分:0)

您正在使用keyup事件,这似乎是问题所在。

如果有任何问题,您需要在输入一个字符后再等待。

更好的解决方案可能是采用与JQuery AutoComplete COmponent相同的策略。

答案 3 :(得分:0)

为了解决您的问题并节省Ajax调用量,我编写了以下示例。此示例允许您处理以下两种情况:

情况1:

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

情况2:

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

在下面的示例中,不需要中止或忽略Ajax调用,您不会向Ajax调用发送垃圾邮件,并且您正在使用Object来处理您的工作。 (我甚至jsFiddled给你)

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 );

});

希望这有帮助。

答案 4 :(得分:-1)

Ajax是一种异步类型,它不推荐你在每个keyup事件上发送请求,试试......

async: false

在post方法中...它会暂停后续帖子,直到当前请求完成其回调

答案 5 :(得分:-1)

实际上你需要一个setTimeout方法来防止多余的ajax调用被触发。

clearTimeout(timer);

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

这应该可以根除你的问题。