Ajax promises without jQuery

May 4, 2020    Development

Ajax promises without jQuery

I originally posted on GeeksWithBlogs.net on 10/07/2014. I decided to port it here, after getting another up-vote on my StackOverflow answer.

I’m trying to move away from jQuery, since I’m using Knockout and AngularJS more all the time. I’ve always done my AJAX calls with the useful jQuery $.ajax( method. Searching for “ajax without jquery” brings up a lot of results, but they all include how to do it with jQuery. First a friend found a tutorial on ES6 promises on HTML5 rocks that included an example of “Promisifying XMLHttpRequest”. Since promises aren’t in all browsers yet (IE and Android Browser) you can polyfill with es6-promise. Or use Q or Bluebird for a promise implementation. Angular JS has it’s own $http and $q to handle this. There is also a question on StackOverflow that I added this answer to. Wrap that in a dataService.js file and I’ll be well on my way to no longer needing jQuery for Ajax calls and still have promises! Update: This project on Github has a simple wrapper of XHR with promises added in. Update 2: Fetch is going to be an XMLHttpRequest replacer though was a ways off in browser support, but now is in all but IE 11.



comments powered by Disqus

Please consider using Brave and adding me to your payment ledger. Then you won't have to see ads!

Support me and download Brave!

Use Brave