An Introduction to the XMLHttpRequest

20/07/24

myrequest.js

const req = new XMLHttpRequest()request.open('GET', '../mydata.json')req.send()console.log(request.response)

lets take a very basic look.

The constructer function XMLHttpRequest() returns an object. The object contains properties, events, methods and more. One such method is the open method. The open method initializes a new HTTP request. It takes two mandatory parameters, the HTTP Request Method, and the URL to the source of the data. Calling the open method will initialize the request, and behind the scenes will store in memory the options that you have chosen. To send our request, we simply need to call the send() method on our object. This will mutate our variable, req, to now include the data from our request in the response property.