javascript – 打印出api回调时获取未定义/无结果

我正在尝试运行我的脚本,该脚本将从电影数据库中搜索电影标题.我在控制台中得到结果,没有错误.但是在我的renderMovies函数中它应该在我的变量中存储API电影标题,情节等,但是当我在列表中打印它时它或者给我什么(空白)或未定义.我是jQuery,
AJAX和API的新手,所以我正在遵循指南,因此代码并非完全由我编写.

OBS:我在使用这个$(“< td>”情节“< / td>”)时未定义,但在使用$(“< td>”)时附加了空白.附加(标题).您可以在renderMovies函数的中间找到该代码.

例如:我搜索电影“阿凡达”,我得到两个结果.但是,两个结果在绘图描述中“存储”为未定义,而标题中为空白.

$(document).ready(function(){
  $(init);

  function init() {
    $("#searchMovie").click(searchMovie);   
    var movieTitle = $("#movieTitle");
    var table = $("#results");
    var tbody = $("#results tbody");

    function searchMovie(){
      var title = movieTitle.val();

      $.ajax({
        url: "http://www.myapifilms.com/imdb/idIMDB?title=" + title + "&token=b81c6057-20cf-4849-abc4-decbf9b65286&format=json&language=en-us&aka=0&business=0&seasons=0&seasonYear=0&technical=0&filter=2&exactFilter=0&limit=1&forceYear=0&trailers=0&movieTrivia=0&awards=0&moviePhotos=0&movieVideos=0&actors=0&biography=0&uniqueName=0&filmography=0&bornAndDead=0&starSign=0&actorActress=0&actorTrivia=0&similarMovies=0&adultSearch=0&goofs=0&keyword=0&quotes=0&fullSize=0&companyCredits=0&filmingLocations=0", 
        dataType: "jsonp",     
        success: renderMovies
      });
    };

    function renderMovies(movies) {
      console.log(movies);
      tbody.empty();

      for(var m in movies) {
        var movie = movies[m];
        var title = movie.title;
        var plot = movie.simplePlot;
        var posterUrl = movie.urlPoster;
        var imdbUrl = movie.urlIMDB;

        var tr = $("<tr>");
        var titleTd = $("<td>").append(title); // blank 
        var plotTd = $("<td>" + plot + "</td>"); // undefined on my website

        tr.append(titleTd);
        tr.append(plotTd);

        tbody.append(tr);
      }
    }
  }
});

最佳答案 我重新排序了你的函数和调用,因为有些变量是未定义的. (谷歌浏览器 – > F12(打开开发者控制台))

这会在按钮单击时返回响应.

    $(document).ready(function () {

function searchMovie() {
    var movieTitle = $("#movieTitle");
    var title = movieTitle.val();

    $.ajax({
        url: "http://www.myapifilms.com/imdb/idIMDB?title=" + title + "&token=b81c6057-20cf-4849-abc4-decbf9b65286&format=json&language=en-us&aka=0&business=0&seasons=0&seasonYear=0&technical=0&filter=2&exactFilter=0&limit=1&forceYear=0&trailers=0&movieTrivia=0&awards=0&moviePhotos=0&movieVideos=0&actors=0&biography=0&uniqueName=0&filmography=0&bornAndDead=0&starSign=0&actorActress=0&actorTrivia=0&similarMovies=0&adultSearch=0&goofs=0&keyword=0&quotes=0&fullSize=0&companyCredits=0&filmingLocations=0",
        dataType: "jsonp",
        success: renderMovies
    });
}

function renderMovies(movies) {
    console.log(movies);
    var movieInfo = movies.data.movies;

    var table = $("#results");
    var tbody = $("#results tbody");

    tbody.empty();

    for (var m in movieInfo) // Tar information från apin och stoppar in i egna variabler.
    {

        var movie = movieInfo[m];
        var title = movie.title;
        var plot = movie.simplePlot;
        var posterUrl = movie.urlPoster;
        var imdbUrl = movie.urlIMDB;

        var tr = $("<tr>");
        var titleTd = $("<td>").append(title); // blank
        var plotTd = $("<td>" + plot + "</td>"); // undefined on my website

        tr.append(titleTd);
        tr.append(plotTd);

        tbody.append(tr);

    }

}

$("#searchMovie").click(searchMovie);

});
点赞