I'm already using a number of plugins and don't want to add more overhead
for something as simple as calling a method on a native javascript object.

I was looking over the documentation for $.ajax and found this:

$.ajax() returns the XMLHttpRequest that it creates.


http://docs.jquery.com/Ajax/jQuery.ajax

This should make it easier to call abort without the need of an extra
callback.

var xhr = $.ajax({
    /* ... */
});

$.('button.cancel').click(function(event)
{
    if (null !== xhr) {
        xhr.abort();
        xhr = null;
    }
    event.preventDefault();
});

--
Hector


On Wed, Aug 26, 2009 at 9:16 AM, Cesar Sanz <the.email.tr...@gmail.com>wrote:

>  Why to re-invent the wheel?
>
> ----- Original Message -----
> *From:* Hector Virgen <djvir...@gmail.com>
> *To:* jquery-en@googlegroups.com
> *Sent:* Wednesday, August 26, 2009 9:43 AM
> *Subject:* [jQuery] Re: How to cancel an AJAX request?
>
> Thanks, Rupak, but I was hoping to be able to accomplish this without a
> plugin. What I'm thinking of doing is storing the XMLHttpRequest object as a
> local variable early on, like in the beforeSend callback.
> var xmlHttpRequest = null;
>
> $.ajax({
>     /* ... */
>     beforeSend: function(request)
>     {
>         xmlHttpRequest = request;
>     }
> });
>
> $.('button.canel').click(function(event)
> {
>     if (null !== xmlHttpRequest) {
>         xmlHttpRequest.abort();
>         xmlHttpRequest = null;
>     }
>     event.preventDefault();
> });
>
> I am not sure if this would cause any issues with the overall ajax
> lifecycle. For example, if the cancel button was clicked, would the "error"
> callback be executed? I'll have to test it and see.
>
> --
> Hector
>
>
> On Tue, Aug 25, 2009 at 9:00 PM, rupak mandal <rupakn...@gmail.com> wrote:
>
>> Hi Hector,
>> you have to use abort function.
>>
>> go through this link http://www.protofunc.com/scripts/jquery/ajaxManager/
>>
>>
>> Thanks
>> Rupak
>>
>>
>> On Wed, Aug 26, 2009 at 2:25 AM, Hector Virgen <djvir...@gmail.com>wrote:
>>
>>> Hello,
>>> Is there a way to stop or cancel an ajax request on demand, similar to
>>> pressing "stop" in the browser? I would like to provide the user with a
>>> cancel button on a "loading" dialog that, when clicked, closes the dialog
>>> and cancels the ajax request. I have the dialog working (thanks to
>>> jQuery-UI) but pressing cancel only closes the it.
>>>
>>> I do not want simply ignore the response because I have a "success"
>>> callback that does some heavy manipulation of the response HTML that I'd
>>> like to prevent from running when the user cancels.
>>>
>>> Any help would be appreciated. Thanks!
>>>
>>> --
>>> Hector
>>>
>>
>>
>

Reply via email to