XML in the Browser: Submitting forms using AJAX - Using POST
(Page 5 of 6 )
Using POST with the request object is a little different. One thing that does not need to change is the format and encoding of the actual information you are sending. In this case, what was the query string is put into the body of the request as an argument to the send method, and the URL remains as an argument to the open method.
var str = "pet=dog";
var url = "http://www.mydomain.com/index.php";// No question mark needed
xmlReq.open("POST",url,true);
xmlReq.send(str);
You will notice if you test this with PHP that the information from this request will not come up in the $_POST or $HTTP_POST_VARS global variables. To access it, you must use $HTTP_RAW_POST_DATA, depending on your php.ini settings. The form of the information will be the string that you sent with the request. In order to use it as key-value pairs, you would have to manually split the string. How this works in ASP, ASP.net, JSP and other server platforms will vary. In order to get the request body values to show up in $_POST as they would from a normal form, you will need to use an additional method of the request object called setRequestHeader().
var str = "pet=dog&hobby=painting";
var url = "http://www.mydomain.com/index.php";// No question mark needed
xmlReq.open("POST",url,true);
xmlReq.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded; charset=UTF-8");
xmlReq.send(str);
Using this header lets the server know that the string of characters in the body of the request is an encoded string of key-value pairs. The information is then handled in the same way as a normal POST form submission.
The open, setRequestHeader and send methods assemble the proper HTTP headers from the information you provide them. The first argument of setRequestHeader is the type of header; the second is the header itself. There is no need to add a ":" between the two, as this is done by the method itself. The header set with this method can be any valid HTTP header.
Next: Submitting a Form >>
More XML Articles
More By Chris Root
|
| · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | | |
|