client side xmlHttpRequest for scripting http requests in a tired yet true way
These days there are a ton of options for scripting http requests with javaScript when it comes to modern native options like fetch, as well as popular user space options like axios. Many developers go so far as to make there own http clients themselves when it comes to yet another option, but even then a native method of one sort or another will have to be used. Call be old fashion by I still find myself using XMLHttprequest for these tasks in many simple pet projects at least.
It does have it’s draw backs, compared to more modern solutions, but it is not that hard to quickly make a solution that makes use of more modern javaScript features like promises. It is true however that if I am going to start to invest a great deal of time into making my own http client, I should probably stop there are many great solutions out there all ready and I am not sure if I really need to be yet another developer re inventing the wheel with this one.
Still if I do choose to make my own custom tailored http client I will most likely use XMLHttpRequest as a way of making the request, and the client will just be a modernized, or tired yet true abstraction of that method.
1 - Using an XMLHTTPRequest pollyfill
There was once a time where the use of a pollyfill for XMLHttpRequest was a must, today more often then not it might not be as big of a deal. Of course it really comes down to browser share of your site, for me it does not seem to matter everyone is using late versions of IE, when they are using IE, which is not often.
Still If it is desired to push backward compatibility as far back as possible a pollyfill like this might be used.
|
|
This is only really needed if for some reason you want to march backward comparability of your project all the way back to IE 5. Maybe if for some reason you get a lot of traffic from china, or some country where there are still a lot of people using these browsers a Polly fill will be of interest.
However if you site analytics show nothing but IE 7, and older chances are there is not much of a reason to bother with the polly fill anymore, and you can just assume that it is there in window to work with.
In which case you can just use the constructor.
|
|
2 - Using XMLHttprequest to make a method for scripting http
I often prefer to make some kind of easy to use method that can be used with just one or two arguments, but can also be given additional things to work with via an options object, just like that of the popular solutions like axios.
So I might end up with something like this.
|
|
So then I can use it by just giving a single string, and a callback in a very tired yet true fashion like this:
|
|
Or the same request can be made by giving an object like this.
|
|
If I want to do something advanced with post requests or something involving custom headers, I can always give a custom beforeSend, and if necessary send method.
|
|
This should be the goal when making any kind of project like this. If I am making a simple get request I should only have to give a url, and a callback. However if I do need to do something more advanced with custom content types, and payloads I can do that without hacking over the source code.
3 - Using a fetch pollyfill
Of course you could do what I just did, and throw together your own solution, but it might be best to just use something that is out there all ready, and see that it confroms to some kind of newer standard for this sort of thing. Because fetch is poised to be the new replacement for XMLHttprequest it might be a good idea to make (or find) some kind of pollyfill that does a good job of bringing fetch to older browsers. for that you might want to check out fetch.js.
4 - Conclusion
XMLhttprequest is the best solution for scripting http if you care about trying to get your code to work on a wide range of browsers, as it is the tired yet way of doing it. For the most part I would not loose sleep over it thought if you choose to go with something more modern.