It could be considered as a general or a simple question but I really need to find out the best practice for handling ajax calls. I’ve seen a lot of different implementations and I’m not sure to choose which. A few requirements of my project is the following ;
- The project is simply a social network startup. Therefore, I need an efficient data format to interpret and handle the data in the return. I chosed json type for efficiency purposes.
- I’m handling the back-end ajax calls by using php. The ajax calls have such functions like add_task, follow_user, unfollow_user, comment_task, like_task, etc.
I’m not sure to choose an elegant way of requests. For instance, I’m not sure whether I should use GET or POST request to ajax_service.php. I’m also not sure to make my request data type (which will be sended to php file that handles ajax requests) as json or normal(just an array of data) get type.
Can you guys guide me which data type and which type of request should I use in this project ?
Generally, HTTP POST is used when you want to write/modify something to the server, GET when you want to read.
Data exchanged between PHP and JavaScript should be encoded as JSON.
You’ll detect ajax requests by reading the
X-Requested-Withheader. If it’s value isXMLHttpRequestthen its an ajax request.