9

Ajax promises without jQuery

 3 years ago
source link: https://www.aligneddev.net/blog/2020/ajaxpromiseswithoutjquery/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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.



About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK