Last active
August 19, 2023 12:48
-
-
Save vsim1964/624225f56e9356701004cee92d905091 to your computer and use it in GitHub Desktop.
AJAX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fetch('ваш_url', { | |
method: 'GET', | |
headers: { | |
'Content-Type': 'application/json', | |
'Authorization': 'Bearer ваш_токен' | |
} | |
}) | |
.then(response => { | |
if (!response.ok) { | |
throw new Error(`Ошибка сети: ${response.status} ${response.statusText}`); | |
} | |
return response.json(); // Преобразование полученных данных в JSON | |
}) | |
.then(data => { | |
// Обработка полученных данных | |
console.log('Полученные данные:', data); | |
// Дополнительные действия с данными могут быть выполнены здесь | |
}) | |
.catch(error => { | |
console.error('Произошла ошибка:', error); | |
}); | |
// * ВАРИАНТ С ПЕРЕМЕННЫМИИ | |
// URL, по которому будет выполнен GET запрос | |
const url = 'https://api.example.com/snippets/1'; | |
// Опции для GET запроса | |
const options = { | |
method: 'GET', // Метод запроса | |
headers: { | |
'Content-Type': 'application/json' // Заголовки запроса | |
// Дополнительные заголовки могут быть добавлены здесь | |
} | |
}; | |
// Выполнение GET запроса с использованием функции fetch | |
fetch(url, options) | |
.then(response => { | |
if (!response.ok) { | |
throw new Error(`Ошибка сети: ${response.status} ${response.statusText}`); | |
} | |
return response.json(); // Преобразование полученных данных в JSON | |
}) | |
.then(data => { | |
// Обработка полученных данных | |
console.log('Полученные данные:', data); | |
// Дополнительные действия с данными могут быть выполнены здесь | |
}) | |
.catch(error => { | |
console.error('Произошла ошибка:', error); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment