ajaxComplete, XMLHttpRequest is undefined
Asked Answered
T

1

7

I have a global ajaxComplete handler:

 $('body').ajaxComplete(function (event, request, settings) {           
        if (request.getResponseHeader('REQUIRES_AUTH') === '1') {
            alert("unauthorized");                
        };
    });

The problem in that the request always in undefined, is filled only event.
Can you explain me why?

Example of ajax request:

$.ajax({
        cache: false,
        data: "GET",
        url: url,
        success: function (content) {           
            $('#modal').html(content);
            $('#modal').modal();           
        }
    });

UPDATE: From the API docs (Thanks to Austin Mullins):
As of jQuery 1.8, however, the .ajaxComplete() method should only be attached to document.

I have change my code to this:

$(document).ajaxComplete(function (event, request, settings) {            
        if (request.getResponseHeader('REQUIRES_AUTH') === '1') {
            alert("unauthorized");               
        };
    });

But now I get the error:

TypeError: document.createDocumentFragment is not a function    
safeFrag = document.createDocumentFragment(); (jquery-1.9.0.js (line 5800))

Browser is Firefox 19.0.2

SOLUTION: The problem was in the Jquery version 1.9.0. I have updated to 1.9.1 and the error is gone. Thanks to Boaz.

Transverse answered 13/3, 2013 at 4:29 Comment(20)
From the API docs: >Note: As of jQuery 1.8, however, the .ajaxComplete() method should only be attached to document.Managerial
@AustinMullins: I have change to document, but now I get the error TypeError: document.createDocumentFragment is not a functionTransverse
How did you change it? It should be $(document).ajaxComplete().Managerial
@AustinMullins: yes, I make it like that.Transverse
in the handler callback can you add console.log(arguments) and check the consolePleuropneumonia
@ArunPJohny: nothing happens, breakpoint not work also. It looks as if the error occurs first.Transverse
Just to make sure - you mean the success handler is not executed either?Peaked
the console will indicate which file and line threw that errorPleuropneumonia
@Boaz: no, success is executed. I mean ajaxComplete handler not executed at all.Transverse
@Transverse Have you tried the .getResponseHeader() method of the xhr object returned to the success object?Peaked
which is the browser usedPleuropneumonia
can you remove the success handler from the ajax call and test it oncePleuropneumonia
@Boaz: I tryed, xhr object is returned in the success. all fine - status 200. getResponseHeader works also.Transverse
@ArunPJohny OP states Firefox 19.0.2Peaked
@Transverse I think the problem is in the success handler, can you remove the dom manipulation code from the success handler and try againPleuropneumonia
It seems you're using jQuery 1.9.0. There were several AJAX-related bugfixes in jQuery 1.9.1. Try using the latest jQuery release, just to rule this out as the cause.Peaked
@Boaz: Thank you. I have update the jquery to the last version and the error is goneTransverse
No problem. Posted it as an answer.Peaked
@Boaz, we do not need three tags to discuss issues in jQuery 1.9.Bliss
@Bliss Considering that jQuery 1.9 is a major release that introduced numerous core changes, I thought it'll be helpful to distinct between 1.9.0 and 1.9.1 which introduced many bugfixes to problems naturally caused by such a major release. But I yield to your judgement, if you don't believe it's necessary.Peaked
P
2

Following your edits, it seems you're using jQuery 1.9.0. There were several AJAX-related bugfixes in jQuery 1.9.1. Try using the latest jQuery release.

Peaked answered 13/3, 2013 at 6:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.