weixin_33686714 2018-05-22 07:55 采纳率: 0%
浏览 136

XHR的异步请求

I need to write async Request to find according article in a search input. I use API of Unplash.com and New York Times. Code doesn't show any errors in dev tools. I've attached codepen link:

Codepen demo

(function(){
  const form = document.querySelector('#search-form');
  const searchField = document.querySelector('#search-keyword');
  let searchedForText;
  const responseContainer = document.querySelector('#response-container');

  form.addEventListener('submit', function(e) {
    e.preventDefault();
    responseContainer.innerHTML = '';

    function addArticles () {}

    function addImage() {}

   const imgRequest = new XMLHttpRequest();
   imgRequest.onload = addImage;
   imgRequest.onerror = function(err) {
     requestError(err, 'image');
   };
   imgRequest.open('GET', `https://api.unsplash.com/search/photos?page=1&query=${searchedForText}`);
   imgRequest.setRequestHeader('Authorization', 'Client-ID <27182>');
   imgRequest.send();

   const articleRequest = new XMLHttpRequest();
   articleRequest.onload = addArticles;
   articleRequest.onerror = function (err) {
     requestError(err, 'articles');
   }
   articleRequest.open('GET', 
   `http://api.nytimes.com/svc/search/v2/articlesearch.json? 
   q=${searchedForText}&api-key=<4c32dbc91636470fb8e286e60041dca5>`);
   articleRequest.send();

  });
});
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#wireshark#的问题:并且能够给数据做标注,如这个流量是在看视频或者是在转账
    • ¥90 请问,这个视频播放软件的名称,用过的朋友请给答案,上方..avi是啥意思?是看短剧还是播放本地视频?
    • ¥15 运筹优化,gurobi,python
    • ¥15 基于python的电影系统推荐
    • ¥20 springmvc重定向和返回json
    • ¥15 数学建模——参会安排怎么做
    • ¥15 电脑键盘实现触摸功能
    • ¥25 matlab无法将表达式转换为双数组怎么解决?
    • ¥15 单片机汇编语言相关程序
    • ¥20 家用射频美容仪技术规格