Search code examples
javascriptdjangoxmlhttprequestdajaxice

XMLHttpRequest emits onreadystatechange but not onload event (using Dajaxice)


I use this code to send a request for an audio file:

    request = new XMLHttpRequest()
    request.open("get", 'http://localhost:8000'+url, true)
    request.responseType = "arraybuffer"

    request.onProgress = ()->
        console.log 'progress...'
        return

    request.onload = ()->
        console.log 'load...'
        return

    request.onerror = ()->
        console.error 'error'
        return

    request.onreadystatechange = (event)->
        if request.readyState == 4
            if request.status == 200
                console.log "200"
            else
                console.log "error"
        return

    request.send()

It reaches onreadystatechange with request.readyState == 4 and request.status == 200, but it never enters the onload callback.

(Because of this, Howler is not working properly.)

I use django dajaxice.

On the server side, I use django with SocketIOServer and everything seems ok, it logs:

127.0.0.1 - - [2014-11-24 11:18:26] "GET /static/sounds/space_ship_engine.mp3 HTTP/1.1" 200 689789 0.099860

(On Chrome 40.0.2214.10 beta (64-bit), on OS X Yosemite 10.10.1)

--- Update ---

I need request.response from the onload callback to use with audioContext.decodeAudioData() (see here), whereas in onreadystatechange I only have request.responseText.


Solution

  • This is due to dajaxice which modifies my XmlHttpRequest.

    I managed to save the original XMLHttpRequest object into a variable (for example window.OriginalXmlHttpRequest = XmlHttpRequest) before loading dajaxice, and then use my variable to de the request.

    Another solution would be to get rid of dajaxice.