|Getting Started With jQuery - Advanced Ajax|
|Written by Ian Elliot|
|Thursday, 04 November 2021|
Page 5 of 5
Other HTTP Methods
jQuery only provides short cut functions for get and post. The reason is that these two are mostly what you want to use but there are other HTTP methods. HTTP/1.0 defined GET, POST and HEAD. We have covered GET and POST in detail.
HEAD operates exactly like GET but it only returns the response headers i.e. there is no response body. You can use this to find out the status of the web page without having to download all its content. For example:
If you try this out using a development server such as the one provided with NetBeans of the PHP server you will discover it doesn't work. Simple development servers usually don't support anything other than GET and POST. When you go beyond GET, POST and HEAD then most production servers have to be configured to respond to them.
HTTP/1.1 introduced OPTIONS, PUT, DELETE, TRACE and CONNECT. Of these only PUT and DELETE are often used.
PUT is like a POST only the URL provided isn't the program you want to process the data it is the URL you would like the data to be stored under. If the file exists then the data is used to update it and if it doesn't the server should create it. Obviously you can't simply let any client create or overwrite files on the server and different servers have different ways of controlling PUT. For example the Apache server lets you specify a program that is called to handle the request. You can think of this as POST but with a fixed program to do the data processing. If you are playing by the rules then you should take the data that has been sent and save it under the file name specified by the URL. Here is a PHP program that does exactly this:
The only thing you need to know to understand this program is that PUT raw data comes in from php://input. The PUT data is stored in a file called puttemp so that it can be validated before being renames to what ever the user specified. You also need to add
to the configuration file inside a <Directory> block.
The important thing to realize is that PUT only works if the server is setup to handle it and there is nothing to say that the server has to play by your expectations. A server should save or update the resource indicated by the URL and the data but it doesn't have to.
To use jQuery to send a PUT you would use something like:
Notice that a PUT doesn't return any data.
DELETE is supposed to delete the file at the specified URL but of course no server would do this as a default action. As for PUT Apache requires a fixed script to be used. So for example if you wanted to enable PUT and DELETE on the entire website (not a good idea) then you would use:
In each case you would have to program the handler check the validity of what was about to happen. In most cased the DELETE handler would delete the specified file but as for PUT this is not enforced. You can do what ever you want in the handlers.
The final three TRACE, OPTIONS and CONNECT are rarely encountered. TRACE echos the request back the client for testing and debugging purposes. OPTIONS returns the methods that the sever supports for that URL. Finally CONNECT requests a connection to a TCP/IP tunnel. All of these require web server configuration to enable them.
Promises are still relatively new and this means that there are asynchronous functions that don't make use of them. This leads on to the need to promisify existing and future code. In this extract fro [ ... ]
or email your comment to: email@example.com