XML in the Browser: Submitting forms using AJAX - HTTP Methods
(Page 4 of 6 )
The request object supports both POST and GET as well as other HTTP methods.
Using the GET method is the simplest to implement.
In a GET request, all information is contained in the URL. It is encoded to escape characters and spaces that are not allowed in URLs by representing them as their hexadecimal equivalents preceded by "%". The information is formatted as key-value pairs in the following way.
http://www.mydomain.com?id=215&status=true
To encode a value you can use the Javascript function escape(). To encode an entire URL, use encodeURI. Keep in mind that encodeURI is part of Javascript 1.5, and is only supported by browsers that also support this version of Javascript.
Once you have a properly formatted string of values that has been tacked onto the end of the URL, you can use the open method of the request object to start the process of sending the request.
var str = "http://www.mydomain.com/index.php?pet=dog&hobby=painting";
xmlReq.open("GET",str,true);
The variable str is shown here as a hard coded string, however in practice it's likely that you will be compiling it from other code. The open method takes three arguments. The first is the HTTP method as a string. The second is the URL string, and the third is a boolean value. This third parameter determines whether or not the request is done asynchronously. If the need arises to ask the user to wait for a request to finish, you could set this argument to false; otherwise, it should be set to true.
The send() Method
The send method is the final stage for sending the request. It takes one argument but in the case of a GET request that value is not needed. Instead this argument should be null.
xmlReq.send(null);
If you were using the POST method, the argument for send() can be a string or an XML DOM object. This allows you to send a string of text or a serialized XML document as part of the body of your request.
Next: Using POST >>
More XML Articles
More By Chris Root
|
| · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | | |
|