MyTetra Share
Делитесь знаниями!
How to make an AJAX call without jQuery?
Время создания: 12.01.2018 13:13
Автор: br0ke
Текстовые метки: javascript, ajax, api, rest, request, query, jquery, js
Раздел: Информационные технологии - JavaScript
Запись: and-semakin/mytetra_data/master/base/1515744837hbx2xhcd9z/text.html на

How about this version in plain ES6/ES2015?

function get(url) {

return new Promise((resolve, reject) => {

const req = new XMLHttpRequest();'GET', url);

req.onload = () => req.status === 200 ? resolve(req.response) : reject(Error(req.statusText));

req.onerror = (e) => reject(Error(`Network Error: ${e}`));




The function returns a promise. Here is an example on how to use the function and handle the promise it returns:


.then((data) => {

// Do stuff with data, if foo.txt was successfully loaded.


.catch((err) => {

// Do stuff on error...


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 with JSON.parse().

Так же в этом разделе:
MyTetra Share v.0.58
Яндекс индекс цитирования