306
Chapter 5
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 can send a lot more than plain text in a POST
request... as you’ll nd 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.
You need to set the content type
Servers get information from the
browser using request headers.
Web Browser
Internet Explorer
Firefox
Opera
Safari
Mozilla
<script>
var request...
function foo()
{
...
}
</script>
request.setRequestHeader(
“Content-Type”, “application/x-www-form-urlencoded”)
request headers
Hypertext Transfer Protocol
POST /placeOrder.php HTTP/1.1
Request Method: POST
Request URI: /placeOrder.php
Request Version: HTTP/1.1
Host: www.headrstlabs.com
Keep-Alive: 300
Connection: keep-alive
Content-Type:
application/x-www-form-urlencoded
Content-Length: 121
Here’s the request your
JavaScript sends to the server.
This is the request header
you added to the ...