Communicating with the server
Many people use third-party libraries to make any request to a server. But do we need these libraries? Let's examine in the following how AJAX can be used natively and what will be the next communication API.
XHR
XMLHttpRequest (XHR) is the main API in JavaScript to exchange data between client and server. XHR was firstly presented by Microsoft in IE5 via ActiveX (1999) and had a proprietary syntax in IE browser until version 7 (2006). This led to compatibility issues that called forth the rise of AJAX-libraries such as Prototype and jQuery. Today, support for XHR is consistent across all the major browsers. In general, to perform an HTML or HTTPS request, we have to do a number of tasks. We create an instance of XHR, initialize a request via open method, subscribe listeners to request-dependent events, set request headers (setRequestHeader
), and eventually call the send method:
var xhr = new XMLHttpRequest(); xhr.open( "GET", "http://www...