How to make an AJAX call using JavaScript, without using jQuery?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- Carriage Return (ASCII chr 13) is missing from tex
- void before promise syntax
- Keeping track of variable instances
I was looking for away to include promises with ajax and exclude jQuery. There's an article on HTML5 Rocks that talks about ES6 promises (could polyfill with a promise library like Q) then use the code snippet that I copied from the article.
Note: I also wrote an article about this.
How about this version in plain ES6/ES2015?
The function returns a promise. Here is an example on how to use the function and handle the promise it returns:
If you need to load a json file you can use
JSON.parse()
to convert the loaded data into an JS Object.You can also integrate
req.responseType='json'
into the function but unfortunately there is no IE support for it, so I would stick withJSON.parse()
.This may help:
in plain JavaScript in the browser:
Or if you want to use Browserify to bundle your modules up using node.js. You can use superagent:
Using the following snippet you can do similar things pretty easily, like this:
Here is the snippet: