views:

280

answers:

2

Hello. From what I understand, if client side javascript code uses the XMLHttpRequest to make a POST request, it waits for a response and when it completes it changes its readystate to ReadyState.Complete and invokes the callback function.

My question is: How does it determine that the response is complete? Does it return the contents of the first TCP packet it gets? Or does the server close the tcp connection hence completing the transaction? Or is there something special in the TCP packet contents that tells the XMLHttpRequest object that the response is complete?

+1  A: 

It either waits for the server to close it's connection, or it waits until it's read a number of bytes from the response equal to the Content-Length header in the response.

MiffTheFox
+1  A: 

It follows HTTP spec. Response must have either Content-Length or use chunked content encoding. In HTTP/1.0 closing connection is an option too.

porneL