Wednesday , 22 November 2017
Home / Programming / JavaScript / Request Types – Advanced JavaScript Tutorial
Request Types
Request Types

Request Types – Advanced JavaScript Tutorial

Request types only matter when you need to send data along with your request. HTTP headers are meant to store information about the request and the response like the referrer or date it was sent. If you need to send custom data like names, addresses, or just a really long message, then you use form data.

Let’s take a look at 2 request types. GET and POST.

GET Requests

Clear your script and add this bit of code.

GET requests are requests that are sent with a query in the URL You may see this when you do searches on Google. It’s basically one long URL. This is a GET request. GET requests are handled by server side languages like PHP or Ruby. We won’t worry about handling this data because we’re learning client-side JavaScript.

In our example, we create a function called encodeURLQuery(). This function checks if the URL has a ?. If it does, then we just append an ampersand. An ampersand separates names and values. A ? represents the start of the query. Then we encode the URI with the encodeURIComponet() function. There’s also the encodeURI() function, but this function will not encode a URL if that’s a value in our GET request.

To tell JavaScript we’re using a GET request, then we set the first parameter in the open() method to GET.

POST Requests

Let’s update our script to this.

POST requests don’t hold the names and values inside the URL. Instead, they are sent along with the request as hidden data. In our example, we change our function to encode the data with just the query alone. To send our data, you pass it in as an argument to the send() method.

POST vs GET

GET requests are 2 times faster and they’re easier to work with. POST requests should only be used for when you don’t want the user to see the data being sent and you have one long query. Browsers limit the amount of characters in a URL so a GET request can be cut off if it’s too long. POST requests can send an unlimited amount of data.

Conclusion

For the most part, if you’re working on the client side of things, then you don’t have to worry about working with the data. If you would like to work with the data, then you need to know a sever side language like PHP. For more information  on request types, then go here.

http://www.w3schools.com/tags/ref_httpmethods.asp

About Jasko Koyn

Check Also

Audio

Audio – Advanced JavaScript Tutorial

The video and audio objects both have the same events, properties and methods. So, you …

Leave a Reply

Your email address will not be published. Required fields are marked *

ULTIMATE WORDPRESS DEVELOPMENT COURSE
Want to learn how to create your own custom WordPress plugins and themes? Check out my course!
Get 93% off my course. You'll also learn how to integrate WooCommerce and BuddyPress into your projects. This is a limited time offer
Yes, I would like to get 93% off your course!
No Thanks!
close-link