Как получить JSON данных по ссылке и проанализировать их в HTML - PullRequest
0 голосов
/ 02 марта 2020

Вот моя ручка и там я должен сделать запрос к этой ссылке , получить данные, поместить их в JavaScript и показать на странице, но мой запрос дает пустой ответ. Что может быть не так и как я могу сделать это лучше?

    // Get the modal element

var modal = document.getElementById('simpleModal');

var modalBtn = document.getElementById('modalBtn');

var closeBtn = document.getElementById('closeBtn');

modalBtn.addEventListener('click', openModal);

var requestResultData;

// Listen for a close click
closeBtn.addEventListener('click', closeModal);

//Outside click
window.addEventListener('click', clickOutside);

// Function 

function openModal() {
  modal.style.display = 'block';
  dataRequest();
}

function dataRequest () {
  var xhr = new XMLHttpRequest();

  xhr.onreadystatechange = function() {
    if (xhr.readyState === 4){
         console.log(xhr);
      }
  };
  xhr.open('GET','http://www.omdbapi.com/?i=tt5687270&apikey=480344f1');
  xhr.send();
}

// function to close modal

function closeModal() {
  modal.style.display = 'none';
}

// Function for an outside click


function clickOutside(e) {

  if(e.target == modal) {
      modal.style.display = 'none';
  }

}

1 Ответ

0 голосов
/ 02 марта 2020

Вы можете использовать API выборки

fetch("https://yourwebsite.com/data.json")
.then(res=>res.json())
.then(res=>{
    //do something with the data here
    console.log(res);
});

Подробнее о API загрузки здесь

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...