Всем привет!
Необходимо отправить GET запрос на сервера ВКонтакте, по следующему url:
https://api.vk.com/method/users.get?user_id=210700286&v=5.52&access_token=TOKEN
В гугле нашёл такой способ:
var request = new XMLHttpRequest();
request.open('GET', 'https://api.vk.com/method/users.get?user_id=210700286&v=5.52&access_token=TOKEN',false);
request.onload = function() {
alert(request.responseText);
}
request.send();
Выдаёт ошибку:
networkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load
Может кто знает альтернативные способы как это можно сделать в JS?
Попробовал через Fetch отправить GET запрос:
fetch('https://api.vk.com/method/users.get?user_id=210700286&v=5.52&access_token=TOKEN')
.then((response) => {
return response.json();
})
С Fetch ошибка такая:
1.Access to fetch at **URL**
from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
2. GET **url** net::ERR_FAILED
3. Uncaught (in promise) TypeError: Failed to fetch
fetch('GET', 'https://...', {mode: 'no-cors'}).then(...)
[object Promise]
. а в консоль вывелось это:Uncaught (in promise) SyntaxError: Unexpected end of input at
, строка на которую была вызвана ошибка: ``` alert(response.json()) ;``` в ``` .then((response) => { alert(response.json()); })}```