Section 5.19. The mysterious POST data


5.19. The mysterious POST data

Frank and Jim are onto something. Remember when we talked about the server decoding the POST data from your request? Once the server opens up the POST request data, it doesn't know what type of data it should expect... and servers really don't like mysteries. Let's take a look, and see what happens with the POST data:

 placeOrder.php  Break Neck server POST dataThe server has no idea what type of data is in this POST request... is it an image? text? XML? ?Since the server doesn't know what kind of data it has, it doesn't know how to pass that information on to the placeOrder.php script. placeOrder.php 

OK, I get it. In a GET request, the data is part of the request URL, so it has to be just text. But in a POST request, you can send an image, or XML, or plain text... so we just need to tell the server what to expect.

You need to set the content type

You can send a lot more than plain text in a POST request... as you'll find out in the next chapter. But when a server receives your POST request, it doesn't know what kind of data it's dealing with, unless you tell the server what to expect.

Once the server knows what kind of data you're sending, it can decode the POST data, and handle it properly. In the Break Neck app, that means passing on the text for the address and order to placeOrder.php. .. and making sure customers are getting their pizza once again.

Servers send information to the browser using sponse headers.

 Web Browser Internet Explorer Firefox Opera Safari Mozilla request.setRequestHeader(   "Content-Type", "application/x-www-form-urlencoded") Hypertext Transfer Protocol   POST /placeOrder.php HTTP/1.1This time, we're sending a POST request.     Request Method: POST     Request URI: /placeOrder.php     Request Version: HTTP/1.1   Host: www.headfirstlabs.com   Keep-Alive: 300   Connection: keep-alive   Content-Type:    application/x-www-form-urlencodedThis is the request header you added to the request.   Content-Length: 121Here's the request your JavaScript sends to the server. 

placeOrder.php ? I have the customer's address, phone number, and order. I'm sending them over as name/value pairs via POST, OK?

This time, the request includes a content type along with the request URL and POST data.

 placeOrder.php POST dataThe POST data is the same as before... Web server...but with a content type, the server knows what kind of data to expect. 

Servers get information from the browser using request headers.

 HTTP/1.1 400 Bad Request   Request Version: HTTP/1.1   Response Code: 400 Date: Wed, 01 Mar 2006 21:27:39 GMT Server: Apache X-Powered-By: PHP/4.3.11a Status: No order was received.The server sends back a response header and status code. Connection: close Transfer-Encoding: chunked Content-Type: text/htmlThis is what the server sends back to the browser. Web server Web Browser Internet Explorer Firefox Opera Safari Mozilla request.getResponseHeader("Status") 




Head Rush Ajax
Head Rush Ajax (Head First)
ISBN: 0596102259
EAN: 2147483647
Year: 2004
Pages: 241

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net