Get latest ajax request and abort others

后端 未结 3 1672
鱼传尺愫
鱼传尺愫 2021-01-04 21:50

I have been searching and this problem seems simple but cannot find answer. I have multiple request calling different url. But for each url, I only want the result once and

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-04 22:14

    Instead of using library, you can use Basic jquery Ajax method :

    beforeSend:{}

    For example:

    xhr = jQuery.ajax({
                url: /*Your URL*/
                type: "POST",
                data: {
                  //data
                },
                 /* if there is a previous ajax request, then we abort it and then set xhr to null */
                beforeSend : function()    {           
                    if(xhr != null) {
                        xhr.abort();
                    }
                },
    success:function(){}
    
    });
    

提交回复
热议问题