Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I'd be interested to see if other people have similar go-to vanilla JS? If so, can you share a link here?

This one is mine:

https://github.com/donohoe/content/blob/master/posts/javascr...

(I follow basic ES6 syntax but easy to switch as needed)



A vanilla JS replacement for jQuery's ajax functionality, that worked on older browsers, would be welcome.

Edit: To clarify, a drop in replacement is what I was looking for.


For me the Fetch API is basically that:

https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API

    fetch("http://www.example.com").then(res => res.json()).then(json => {})
Only big downside is no IE11 support but thankfully I don't have to worry about that so much these days.


It still doesn't go POST in a nice way like the jQuery API does BUT it is not worth bringing jQuery to the page just for making requests with slightly nicer syntax.


What’s not nice about POST?

    fetch(url, {
      method: “POST”,
      body: “a body”
    })


It has some limitations as well around timeouts, cross site cookies, and progress tracking (uploads, for example).


Sure, but those aren’t common uses for $.ajax. Fetch can get you 95% of the way there.


How old do you need to go? It looks like most of the standard XHR was working in IE7 except for the withCredentials flag.

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequ...



Added!

I don't know how far back it will help with old browsers. I'd need to test that and it will take awhile.

JSON.parse will throw a few older browsers by itself.


So why not just use jQuery?


I like their ajax implementation, but don't need the rest of the library.


Maybe try Zepto?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: