postData提供无效的URL,而Postman提供有效的URL。在我的本地主机上运行API

时间:2020-04-03 17:11:02

标签: rest api post fetch fetch-api

在此代码中,URL返回数据,使用Object.values()在数组中获取数据的值。该数组由视频链接列表组成。使用邮递员时,链接有效,因为它们是新批处理。当我使用此代码将链接提取到视频播放器时,它会生成过期的URL。我也使用了无缓存。

async function postData(url = '', data = {}) {
  // Default options are marked with *
  const response = await fetch(url, {
    method: 'POST', // *GET, POST, PUT, DELETE, etc.
    mode: 'cors', // no-cors, *cors, same-origin
    cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
    credentials: 'same-origin', // include, *same-origin, omit
    headers: {
      'Content-Type': 'application/json'
      // 'Content-Type': 'application/x-www-form-urlencoded',
    },
    redirect: 'follow', // manual, *follow, error
    referrerPolicy: 'no-referrer', // no-referrer, *client
    body: JSON.stringify(data) // body data type must match "Content-Type" header
  });
  return await response.json(); // parses JSON response into native JavaScript objects
}



postData('http://127.0.0.1:5000/video', {'email': '', 'password': '' })
  .then((data) => {
    var x = Object.values(data);
    console.log(x); // JSON data parsed by `response.json()` call

var mylapse = document.getElementById('timelapse');
var thislapse = x;
var activeVideo = 0;

mylapse.addEventListener('ended', function(e) {
  // update the new active video index
  activeVideo = (++activeVideo) % thislapse.length;

  // update the video source and play
  mylapse.src = thislapse[activeVideo];
  mylapse.play();
});

  });

0 个答案:

没有答案