如何在纯js上写双Ajax请求然后返回一个公共值?

时间:2016-11-21 22:51:30

标签: javascript ajax

我想得到一个结果,允许我从数据库omdbapi.com下载两部电影的数据。接下来比较演员并回归这些电影的共同演员。

非常感谢任何帮助!

movie_serch.addEventListener("click", movieSerch, false);

function movieSerch(){
movieA = document.getElementById("userInput").value,
movieB = document.getElementById("userInput2").value;

requestFirst = new XMLHttpRequest();
requestFirst.open('GET', 'http://www.omdbapi.com/?t='+movieA+'&y=&plot=short&r=json', true);

requestSecound = new XMLHttpRequest();
requestSecound.open('GET', 'http://www.omdbapi.com/?t='+movieB+'&y=&plot=short&r=json', true);

requestFirst.onload = function() {
  if (requestFirst.status >= 200 && requestFirst.status < 400) {
    // Success!
    var respFirst = JSON.parse(this.responseText),
    actors = respFirst.Actors.split(", "),
        director = respFirst.Director.split(", ");
  } else {
    // We reached our target server, but it returned an error

  }
}; 
requestSecound.onload = function() {
   if (requestSecound.status >= 200 && requestSecound.status < 400) {
    // Success!
    var respSecound = JSON.parse(requestSecound.responseText);
    actors = respSecound.Actors.split(", "),
    director = respSecound.Director.split(", ");
    } else {
    // We reached our target server, but it returned an error
  }
};
requestFirst.send();
requestSecound.send();
};

0 个答案:

没有答案