views:

1210

answers:

2

Hello,

I've set a breakpoint in firebug and am examining my xhr object. Its readyState is 0, its status is 200. My understanding is that if the status is 200, the request should have at least started, and the readystate should be at least 1. Can anyone explain what's going on?

I'm using jquery's $.ajax to create the xhr object, but I wouldn't think that would affect the xhr's readystate and status.

thanks,

-Morgan

+4  A: 

The request is complete when readyState is 4, not when the status is 200.

Luca Matteis
Status 200 can happen before you get any meaningful data back: It's just a header.
Nerdling
But does it make sense for readystate to be 0 in that case?
morgancodes
morgancodes: if you set the breakpoint too early, yes.
Luca Matteis
@Luca Fair enough. Thanks. My code works now, I'll consider the mystery as-solved-as-I-need-it-to-be.
morgancodes
+1  A: 

The w3c defines that readyState is a progressive indication of the progress of the xmlhttprequest. It transitions in order from 0->4 (Uninitialized, Open, Sent, Recieving, Loaded) and only at loaded is the content ready to read. This is the point of the property and the reason you monitor the onreadystatechange event and not status which is a response header.

By the same spec status is supposed to raise an exception when not available (at recieving or loaded) but I imagine this is flouted by implementation for ease of use.

annakata
"at recieving or loaded" ? Status should only be available on `loaded`.
Luca Matteis
From the spec: "It MUST be available when readyState is 3 (Receiving) or 4 (Loaded)."
annakata