如何停止已经在后端处理的请求?

时间:2015-09-08 14:45:18

标签: javascript ajax extjs xmlhttprequest

How can i stop an request which is already being processing in backend ? 

I am using EXT Js Framework,There are two button ,
1.Search button
    This button queries the back end and fetches the required JSON output
    Usually it takes a long time to process from the backend .
    In my window i will be seeing the processing dialogue box.
2.Stop Button
    The new requirement what i need here is to stop/abort/cancel
    the existing search request . 
    How can cancel the existing request when stop button is clicked . 
    Do i need to do some coding in Backend or Front End ?
    Any tricks , please help.

以下是代码段

// Search panel with two buttons

    var searchPanel = new Ext.FormPanel({
        frame:true,
        title: 'Search Criteria',
        collapsible:true,
        defaultType: 'textfield',
        region:'west',
        autoScroll:true,

        // has few text box for input search 

        ],
        buttons: [{
                id: 'search-button',
                text:'Search',
                handler:applySearch
            },{
                id:'stop-button',
                text:'Stop',
                handler:stopSearch
            }
        ]
    });

    function applySearch(){
        applySearchFunction('search');
    }


    function applySearchFunction() {
        //calls the store with required fields

    }

    function stopSearch(){
        //What is the hack i need to place here in order to abhort/cancel the request which is already in processing state.
    }



var reader = new Ext.data.JsonReader({
    id:'id'
    ,totalProperty:'total'
    ,root:'rows'
    ,fields:[
        // required fields
    ]
});

function handleServerTimeOutError(conn, response, options) {
        if(response.status == 504){
            Ext.Msg.alert(
                'Timed out',
                'The search operation timed out, please try again'
            );
        }
}
// Back end call is happening here .. 
var connObj = new Ext.data.Connection({
    timeout : toMS(60),
    url : 'file contains json logic',
    method : 'POST',
    listeners: {
        requestexception: handleServerTimeOutError
    }
});

var store = new Ext.data.GroupingStore({
    reader: reader,
    //use proxy so we can set timeout
    proxy : new Ext.data.HttpProxy(connObj),
    //autoLoad: 'true',
    remoteSort: true,
    listeners: {
        beforeload: startIdleTimer
    }
});

2 个答案:

答案 0 :(得分:0)

您可以中止XMLHTTPRequest,但无法保证服务器将停止处理请求。

例如在PHP中,您可以执行:ignore_user_abort(true)。它主要取决于您的服务器技术。

答案 1 :(得分:0)

尝试使用XMLHTTPRequest.abort() 在你的情况下尝试

function stopSearch(){
     connObj.abort();
}