Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does calling abort() on ajax request cause error in ASP.Net MVC (IE8)

I use jquery to post to an MVC controller action that returns a table of information. The user of the page triggers this by clicking on various links.

In the event the user decides to click a bunch of these links in quick succession I wanted to cancel any previous ajax request that may not have finished.

I've found that when I do this (although its fine from the client's POV) I will get errors on the web application saying that

"The parameters dictionary contains a null entry for parameter srtCol of non-nullable type 'System.Int32'"

Now the ajax post deffinately passes in all the parameters, and if I don't try and cancel the ajax request it works just fine.

But if I do cancel the request by calling abort() on the XMLHttpRequest object that ajax() returns before it finishes I get the error from ASP.Net MVC.

Example:

//Cancel any pevious request
if (req)
{
    req.abort();
    req = null;
}

//Make new request
req = $.ajax({
    type: 'POST',
    url: "/Myapp/GetTbl",
    data: {srtCol: srt, view: viewID},
    success: OnSuccess,
    error: OnError,
    dataType: "html"
});

I've noticed this only happen is IE8. In FF it seems to not cuase a problem.

Does anyone know how to cancel an ajax request in IE8 without causing errors for MVC?

Thanks for any help.

like image 255
user169867 Avatar asked May 27 '10 15:05

user169867


People also ask

How do I cancel my ongoing Ajax request?

Sometimes you might want to cancel the ajax request that is pending from long time or want to cancel request if user accidently clicked submit button. In that cases, so you can just use abort() method. If the request has been sent already, this method will abort the request.

Which function is used to cancel the current request in Ajax?

XMLHttpRequest is an object that we use to send an HTTP request to the server to get the required data from the server. XMLHttpRequest provides an abort() method to cancel the sent request to the server. XMLHttpRequest. abort() Method: This method is used to abort or cancel the HTTP request.


1 Answers

IE might be sending part of the request body to the server before aborting, giving the server enough request to start processing, but not all the parameters it needs. You could check this by looking at IE's activity in a proxy (like fiddler)---if you get part of the request, then IE is handling the abort() badly. If that's the case, you may want to let the old AJAX request finish, and just prevent its OnSuccess from doing anything, rather than aborting it.

EDIT:

var req; // current/latest request

var OnSuccess = function(data, textStatus, XMLHttpRequest) {
    if (XMLHttpRequest != req) return;
    // latest request has succeeded
}

req = $.ajax(...);

Keep a reference to the latest request, as you do now, but don't abort it. Instead, OnSuccess checks if the request object passed to it is the same as the "latest request"; if they're different, another request has intervened, and it passes.

like image 65
kevingessner Avatar answered Oct 20 '22 23:10

kevingessner